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

时间:2016-10-28 09:35:30

标签: asp.net-mvc ravendb

以下是我在ASP.Net MVC应用程序的客户端/服务器模式(非嵌入式)中使用RavenDB所采取的步骤。虽然我完全按照步骤操作,但结果并不像预期的那样。如果有任何错误,请纠正我。

  1. 安装RavenDB.Client& RavenDB.Server通过Nuget。
  2. 转到Packages文件夹,启动Raven.Server.exe以使服务运行
  3. 在浏览器中打开http://localhost:8080/,RavenStudio已启动。
  4. 创建了一个名为“testdb”
  5. 的数据库
  6. 我有一个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; }
    }
    
  7. 在我的控制器中,我已初始化文档存储,并打开会话。

    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();
    }
    
  8. 构建解决方案。刷新localhost:8080,仍未插入数据。

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

  10. 提前感谢您的帮助!

    尝试点击调试,它会打开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. 我有管理视图,包含

    4. AddRestaurant, AdminLogin, ManageFoodMenu, ManageOrder, ManageReservation

1 个答案:

答案 0 :(得分: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();
    }
}

当我访问与http://localhost:50791/对应的路径HomeController/Index时,一切都按预期进行:

enter image description here

你能提供一些关于你想做的更多细节吗?