使用applicationuser的一对多

时间:2015-08-16 18:56:39

标签: asp.net-mvc entity-framework asp.net-mvc-5 entity-framework-6 asp.net-identity

如果这是一个dupp,请务必指导我。

ASP.NET,MVC5,EF6

每个应用程序用户都可以拥有许多TfsAccountCredentials

IdentityModel.cs具有以下内容:

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

TfsAccountCredentials.cs具有以下内容:

public class TfsAccountCredentials
{
    public int Id { get; set; }

    //other properties

    [ForeignKey("ApplicationUserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }

    public string ApplicationUserId { get; set; }

}

TfsAccountCredentialsDb具有以下内容:

public class TfsAccountCredentialsDb : DbContext
{
    public TfsAccountCredentialsDb() : base("DefaultConnection")
    {
    }

    public DbSet<TfsAccountCredentials> TfsAccountCredentials { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<TfsAccountCredentials>()
            .HasRequired(ac => ac.ApplicationUser)
            .WithMany(ac => ac.TfsAccountCredentials)
            .HasForeignKey(ac => ac.ApplicationUserId);
    }
}

IdentityDb.cs具有以下内容:

public class IdentityDb : IdentityDbContext<ApplicationUser>
{
    public IdentityDb() : base("DefaultConnection")
    {
    }
}

两件事。首先,当我使用Identity上下文添加迁移时,它生成的脚本的一部分会添加TfsAccountCredentials表,该表由于TfsAccountCredentials上下文的早期迁移而已经存在。

其次,当我使用TfsAccountCredentials上下文添加迁移时,出现以下错误:

TfsTeamStatus.Web.DataContexts.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
TfsTeamStatus.Web.DataContexts.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

修改 我的最终解决方案:

IdentityModel.cs包含以下内容:

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

TfsAccountCredentials.cs包含以下内容:

public class TfsAccountCredentials
{
    public int Id { get; set; }

    //other properties

    [ForeignKey("ApplicationUserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }

    public string ApplicationUserId { get; set; }

}

ApplicationDb.cs包含以下内容:

public class ApplicationDb : IdentityDbContext<ApplicationUser>
{
    public ApplicationDb() : base("DefaultConnection")
    {
    }

    public DbSet<TfsAccountCredentials> TfsAccountCredentials { get; set; }

}

所以TfsAccountCredentialsDb.cs和IdentityDb.cs结合在一起。

2 个答案:

答案 0 :(得分:2)

为什么要分开数据库?我想您希望在相同数据库中将2个表格设为不同的数据库,因此请删除TfsAccountCredentialsDb并将TfsAccountCredentials添加到IdentityDb

public class IdentityDb : IdentityDbContext<ApplicationUser>
{
    public IdentityDb() : base("DefaultConnection")
    {
    }
    public DbSet<TfsAccountCredentials> TfsAccountCredentials { get; set; }

}

答案 1 :(得分:1)

您不需要IdentityDb,因为IdentityDbContext<ApplicationUser>是处理身份问题的上下文。那么你可以:

A)TfsAccountCredentialsDb继承IdentityDbContext<ApplicationUser>。这将允许您在模型中引用ApplicationUser。 ApplicationUser的DbSet位于您无法看到的基类中。

B)如果您希望TfsAccountCredentialsDb继承DbContext,那么您必须在该上下文中添加ApplicationUser的副本及其DbSet您可以在应用程序的上下文中引用它。

相关问题