【问题标题】:seeding user database with one to many relationship使用一对多关系播种用户数据库
【发布时间】:2013-06-21 14:37:51
【问题描述】:

我有一个 MVC4 项目,它使用实体框架 5(代码优先)和代码优先迁移。该网站使用SimpleMembership。我正在尝试播种 users 表,但遇到与导航属性设置相关的错误。

我有一个 AccountUser 模型。 SimpleMembership 使用 User 模型作为我的自定义 users 表。这种关系是一对多的,每个User 有一个Account,但每个Account 有很多Users。我试图在种子时间将User 设置为Account 关系,但我无法做到。


模型(无关属性省略):

public class User
{
    [Key]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Description { get; set; }

    public int AccountId { get; set; }
    public virtual Account Account { get; set; }
}

public class Account
{
    [Key]
    public int AccountId { get; set; }
    public string Name { get; set; }

    // Account has many users but only one "admin" user. ID manually set.
    // Nullable because of chicken / egg problem (user requires account, account requires user)
    public int? AdminUser { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

种子方法:

protected override void Seed(MyProject.Data.MyContext context)
{
    WebSecurity.InitializeDatabaseConnection("MyContext", "Users", "UserId", "UserName", autoCreateTables: true);

    Account adminAccount = new Account()
    {
        Name = "Dev",
        Description = "Dev account"
    };

    context.Accounts.AddOrUpdate(adminAccount);

    if (!WebSecurity.UserExists("dev@dev.com"))
    {
        WebSecurity.CreateUserAndAccount("dev@dev.com", "password", new User()
        {
            Email = "dev@dev.com",
            FirstName = "Dev",
            LastName = "Guy"
            // Problematic properties - see below
            // Account = adminAccount,
            // AccountId = adminAccount.AccountId
        });
    }
}

当我打电话给WebSecurity.CreateUserAndAccount 时,我似乎遇到了介绍问题:

  1. 如果我尝试包含导航属性 Account = adminAccount,则会收到错误消息:

    "No mapping exists from object type MyProject.Models.Account to a known managed provider native type."

  2. 如果我尝试仅包含 FK 引用 AccountId = adminAccount.AccountId 我会收到错误:

    "Invalid column name 'Account'"

  3. 如果我不包括AccountAccountId,我也会得到

    "Invalid column name 'Account'"


我应该指出,我只在我的类中使用导航属性。当我添加外键属性时,一切都爆炸了(存在与 FK 相关的错误,但我无法很好地重现它们以将它们发布在这里,它们可能与我的数据库相关,需要重新创建。我看到了上面- 使用新的无表数据库时出错。

另外,我使用 SQL Azure 作为我的 SQL 数据库。

这似乎是SimpleMembership 的常见用例;我不知道它给我带来了这么多麻烦。

【问题讨论】:

    标签: asp.net-mvc-4 entity-framework-5 azure-sql-database entity-framework-migrations seeding


    【解决方案1】:

    正如我所想,这是一个非常愚蠢的错误。在我尝试将其 ID 分配给新的 User 之前,基本上尚未在数据库中创建 Account

    当我尝试手动将数据插入数据库时​​,出现以下错误:

    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Users_dbo.Accounts_AccountId". The conflict occurred in database "projectdev", table "dbo.Accounts", column 'AccountId'.

    经过一番挖掘,我发现我分配给用户的 AccountID 不正确,当我将 ID 更改为正确的 ID 时,它工作得很好。这让我意识到,在尝试将 AccountId 分配给 Users 表之前,我并没有将 Account 上下文更改提交给数据库。

    我只是在context.Accounts.AddOrUpdate(adminAccount); 之后添加了context.SaveChanges();,一切正常。

    好难过...

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 2019-07-21
      • 2020-02-24
      • 2019-11-17
      • 2017-12-29
      • 2021-12-28
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多