【问题标题】:Using MVC3/EF CodeFirst with Repository Pattern, want to add sample data, getting error将 MVC3/EF CodeFirst 与 Repository Pattern 一起使用,想要添加示例数据,出现错误
【发布时间】:2011-03-24 20:06:14
【问题描述】:

我无法弄清楚如何将示例数据添加到使用 EF Code First 的 MVC3 应用程序中。我在下面粘贴了我的示例代码以及我得到的错误。添加示例数据是否有一个好的模式可以遵循?

任何想法表示赞赏。

 public ViewResult Index()
    {
        return View(usersRepository.GetAllUsers());
    }

    public ViewResult PopulateData()
    {
        PopulateUsersDb();

        return View(usersRepository.GetAllUsers());
    }

    private void PopulateUsersDb()
    {
        Users user1 = new Users()
        {
            CreationDate = DateTime.Today,
            Id = 1001,
            FirstName = "David",
            LastName = "Nadler",
            PlanExpirationDate = DateTime.Now.AddMonths(6),
            Username = "dnadler"
        };
        usersRepository.InsertOrUpdate(user1);
        usersRepository.Save();

错误:

存储更新、插入或删除 声明影响了意外 行数 (0)。实体可能有 之后被修改或删除 实体已加载。刷新 ObjectStateManager 条目。

【问题讨论】:

  • 我认为有趣的部分是您的问题中未显示的InsertOrUpdate 方法的实现。
  • 同意。该错误意味着它没有做任何事情,如果它认为它应该进行更新但记录不存在,则会发生这种情况。

标签: asp.net entity-framework asp.net-mvc-3 repository


【解决方案1】:

检查一下.. 首先使用 ef 代码的存储库模式。也许它可以帮助你。 http://average-uffe.blogspot.com/2011/03/repository-pattern-with-ef-code-first.html

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多