MVC 中的 RavenDB 没有将数据保存到数据库

RavenDB in MVC is not saving data to the database

以下是我为我的 ASP.Net MVC 应用程序在 client/server 模式(非嵌入式)中使用 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. 构建解决方案。刷新localhost:8080,数据还是没有插入。

  8. 虽然我完全按照我学习过的所有教程进行操作,但我不知道我做错了什么。尝试了很多不同的方法,但仍然无济于事。

在此先感谢您的帮助!

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

#更具体#

  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

我真的不知道是什么导致了这个问题。就一个问题,你刚刚编译了项目或者你调用了你的动作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/ 时,一切都按预期进行:

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