下面是我为我的ASP.Net MVC应用程序在客户端/服务器模式(不嵌入)中使用RavenDB所做的步骤。尽管我完全遵循这些步骤,但结果并不如预期。如果有任何错误,请纠正我。MVC中的RavenDB没有将数据保存到数据库中
- 通过Nuget安装RavenDB.Client & RavenDB.Server。
- 进入Packages文件夹,启动Raven.Server.exe让服务运行
- 在浏览器中打开http://localhost:8080/,RavenStudio启动。
- 创建了一个名为“testdb”的数据库
我有一个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; } }
在我的控制器中,我已初始化文档存储以及打开会话。
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(); }
构建解决方案。刷新本地主机:8080,数据仍未插入。
我不知道我做错了什么,虽然我完全按照我所经历的所有教程。如此多的尝试使用不同的方式,但仍然无济于事。
在此先感谢您的帮助!
尝试点击调试,它会打开localhost:33062,但随后会显示服务器错误,如下所示。
#更具体#
我有一个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; } }
我有一个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(); } } }
我有管理视图,包括
AddRestaurant, AdminLogin, ManageFoodMenu, ManageOrder, ManageReservation
public ActionResult Index(){}'是'Controller'右边的'Action'方法吗?这意味着您的项目将对该路线做出响应。试试这个:在你的项目上点击调试。它会启动你的mvc应用程序。然后,如果您不在HomeController中,请添加/ NameOfYouControllerWithoutController。它会创建你的数据库并添加数据! –
我试过调试过。然后它打开localhost:33062,但显示服务器错误。我不知道什么是错的? –
你创建了什么类型的项目? –