Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何首先使用EF代码向aspnet用户添加外键?_Entity Framework_Ef Code First - Fatal编程技术网

Entity framework 如何首先使用EF代码向aspnet用户添加外键?

Entity framework 如何首先使用EF代码向aspnet用户添加外键?,entity-framework,ef-code-first,Entity Framework,Ef Code First,我的代码是: public class UserGroupUser { public int UserGroupUserId { get; set; } public string UserId { get; set; } [Key, ForeignKey("UserId")] public virtual ApplicationUser ApplicationUser { get; set; } } 当我尝试创建一个控制器时,我得到以下错误: EntityTy

我的代码是:

public class UserGroupUser
{
    public int UserGroupUserId { get; set; }
    public string UserId { get; set; }
    [Key, ForeignKey("UserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }
}
当我尝试创建一个控制器时,我得到以下错误:

EntityType“IdentityUserLogin”未定义密钥。请为定义密钥 此实体类型无效

EntityType“IdentityUserRole”未定义键。请为定义键 此实体类型无效

EntityType“IdentityUserLogins”基于 没有定义键

EntityType:EntitySet“IdentityUserRoles”基于 未定义密钥的“IdentityUserRole”

但是我可以在SQLServer管理中看到数据库中所有表的键。
如何解决此问题?

实体需要主键。相应地更改其他型号

public class UserGroupUser
{
    [Key]
    public int UserGroupUserId { get; set; }
    public string UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }
}

事实证明,EF无法识别IdentityUserLogin、IdentityRole、IdentityUserRole表中的键(我不知道原因)。因此,我必须告诉它,通过重写MainContext类中的OnModelCreating方法(从System.Data.Entity.DbContext继承),这些表已经有键了:

public类MainContext:DbContext
{
public MainContext():base(“MyDatabaseConnectionString”){}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasKey(l=>l.UserId);
modelBuilder.Entity().HasKey(r=>r.Id);
modelBuilder.Entity().HasKey(r=>new{r.RoleId,r.UserId});
基于模型创建(modelBuilder);
}
公共数据库集用户组{get;set;}
公共DbSet UserGroupUsers{get;set;}
}

似乎此方法在生成数据库之前执行。

您必须使用
Id
属性或
[Key]
属性为EF实体指定主键。另外,看起来您正在使用ASP.NET标识,是否正确?如果是,请注意不要在导航属性上使用
[Key]
。请在UserId属性上尝试
[Key,ForeignKey(“ApplicationUser”)]
。谢谢@Ppp的回答。你是对的。我的代码中缺少[键]。但事实上,问题是别的。虽然这个问题在其他帖子中已经解决了,但我将把它作为另一个答案来写。
public class MainContext : DbContext
{
    public MainContext() : base("MyDatabaseConnectionString") { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<UserGroup> UserGroups { get; set; }
    public DbSet<UserGroupUser> UserGroupUsers { get; set; }

}