EF6与IdentityDbContext有很多到很多无效的列名

时间:2014-02-09 18:46:29

标签: c# asp.net sql-server entity-framework asp.net-identity

我有一个IdentityDbContext,其中ApplicationUser:IdentityUser有一个双重自引用表来映射不同成员之间的FriendShip:

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<ApplicationUser> Friends { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ApplicationUser>().HasMany(x => x.Friends).WithMany();

        //I've tried the commented out section with no success
        //modelBuilder.Entity<ApplicationUser()
        //.HasMany(x=>x.Friends).WithMany().Map(x=>
        //{ 
        //    x.MapLeftKey("Friend1"); 
        //    x.MapRightKey("Friend2"); 
        //    x.ToTable("Friendship"); 
        //});
        base.OnModelCreating(modelBuilder);
    }

然后我尝试将新用户添加到db:

 void AddUser()
    {
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
        var user = new ApplicationUser { UserName = "HelloWorld" };
        manager.Create(user, "password1");
    }

抛出EntityCommandExecutionException:“无效的列名'ApplicationUser_Id'。” 如果我放弃多对多关系,那一切都有效,但在项目中建立这种关系会很好。

列名是新关系表的默认值,另一个是ApplicationUser_Id1,我认为更改列名可能会有所帮助,但它不起作用(注释掉的部分)。

此奇怪的是,这个列名是导致异常的原因,因为将用户插入数据库与该表无关,我在这里丢失了。

非常感谢任何建议/建议。

1 个答案:

答案 0 :(得分:0)

我找到了答案here

因此,您的映射将是:

modelBuilder.Entity<ApplicationUser()
    .HasMany(x=>x.Friends).WithMany();
相关问题