2016-10-28 143 views
1

下面是我为我的ASP.Net MVC应用程序在客户端/服务器模式(不嵌入)中使用RavenDB所做的步骤。尽管我完全遵循这些步骤,但结果并不如预期。如果有任何错误,请纠正我。MVC中的RavenDB没有将数据保存到数据库中

  1. 通过Nuget安装RavenDB.Client & RavenDB.Server。
  2. 进入Packages文件夹,启动Raven.Server.exe让服务运行
  3. 在浏览器中打开http://localhost:8080/,RavenStudio启动。
  4. 创建了一个名为“testdb”的数据库
  5. 我有一个RestaurantModel.cs。

    internal class RestaurantModel{ 
        public string ResName { get; set; } 
        public string ResAddress { get; set; } 
        public string ResCity { get; set; } 
        public string ResState { get; set; } 
        public int ResPostcode { get; set; } 
        public string ResPhoneNum { get; set; } 
    } 
    
  6. 在我的控制器中,我已初始化文档存储以及打开会话。

    public ActionResult Index() 
    { 
        using (var store = new DocumentStore 
        { 
         Url = "http://localhost:8080/", 
         DefaultDatabase = "testdb" 
        }) 
        { 
         store.Initialize(); 
    
         using (var session = store.OpenSession()) 
         { 
          session.Store(new RestaurantModel 
          { 
           ResName = "TestName", 
           ResAddress = "Test Address", 
           ResCity = "TestCity", 
           ResState = "TestState", 
           ResPostcode = 82910, 
           ResPhoneNum = "02-28937481" 
          }); 
    
          session.SaveChanges(); 
    
         } 
        } 
    
         return View(); 
    } 
    
  7. 构建解决方案。刷新本地主机:8080,数据仍未插入。

  8. 我不知道我做错了什么,虽然我完全按照我所经历的所有教程。如此多的尝试使用不同的方式,但仍然无济于事。

在此先感谢您的帮助!

尝试点击调试,它会打开localhost:33062,但随后会显示服务器错误,如下所示。

enter image description here

#更具体#

  1. 我有一个RestaurantModel.cs

    internal class RestaurantModel 
    { 
        public string ResName { get; set; } 
        public string ResAddress { get; set; } 
        public string ResCity { get; set; } 
        public string ResState { get; set; } 
        public int ResPostcode { get; set; } 
        public string ResPhoneNum { get; set; } 
    } 
    
  2. 我有一个AdminController

    using FYP2.Models; 
    using Raven.Client.Document; 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Mvc; 
    
    namespace FYP2.Controllers 
    { 
    public class AdminController : Controller 
    { 
        // GET: Admin 
        public ActionResult Index() 
        { 
         using (var store = new DocumentStore 
         { 
          Url = "http://localhost:8080/", 
          DefaultDatabase = "foodfurydb" 
         }) 
         { 
          store.Initialize(); 
    
          using (var session = store.OpenSession()) 
          { 
           session.Store(new RestaurantModel 
           { 
            ResName = "Boxer Republic", 
            ResAddress = "NO 2A-G, Jalan BK 5A/2C", 
            ResCity = "Puchong", 
            ResState = "Selangor", 
            ResPostcode = 47180, 
            ResPhoneNum = "03-80748088" 
           }); 
    
           session.SaveChanges(); 
    
          } 
         } 
    
          return View(); 
        } 
    
        public ActionResult AdminLogin() 
        { 
         return View(); 
        } 
    
        public ActionResult AddRestaurant() 
        { 
         return View(); 
        } 
    
        public ActionResult ManageFoodMenu() 
        { 
         return View(); 
        } 
    
        public ActionResult ManageOrder() 
        { 
         return View(); 
        } 
    
        public ActionResult ManageReservation() 
        { 
         return View(); 
        } 
    
    } 
    } 
    
  3. 我有管理视图,包括

AddRestaurant, AdminLogin, ManageFoodMenu, ManageOrder, ManageReservation

回答

1

我真的不知道有什么可以导致此问题。只有一个问题,你只是编译了这个项目,或者你叫你的行动domain:port/yourcontroller/index

我创建了一个MVC项目并复制你的代码:

public class HomeController : Controller 
{ 
    internal class RestaurantModel 
    { 
     public string ResName { get; set; } 
     public string ResAddress { get; set; } 
     public string ResCity { get; set; } 
     public string ResState { get; set; } 
     public int ResPostcode { get; set; } 
     public string ResPhoneNum { get; set; } 
    } 

    public ActionResult Index() 
    { 
     using (var store = new DocumentStore 
     { 
      Url = "http://locaslhost:8080/", 
      DefaultDatabase = "testdb" 
     }) 
     { 
      store.Initialize(); 

      using (var session = store.OpenSession()) 
      { 
       session.Store(new RestaurantModel 
       { 
        ResName = "TestName", 
        ResAddress = "Test Address", 
        ResCity = "TestCity", 
        ResState = "TestState", 
        ResPostcode = 82910, 
        ResPhoneNum = "02-28937481" 
       }); 

       session.SaveChanges(); 

      } 
     } 

     return View(); 
    } 
} 

当我访问这相当于我HomeController/Index路径http://localhost:50791/,一切都如预期:

enter image description here

灿你提供关于你正在尝试做的更多细节?

+0

public ActionResult Index(){}'是'Controller'右边的'Action'方法吗?这意味着您的项目将对该路线做出响应。试试这个:在你的项目上点击调试。它会启动你的mvc应用程序。然后,如果您不在HomeController中,请添加/ NameOfYouControllerWithoutController。它会创建你的数据库并添加数据! –

+0

我试过调试过。然后它打开localhost:33062,但显示服务器错误。我不知道什么是错的? –

+0

你创建了什么类型的项目? –