如果这是一个骗局,请务必告诉我答案。
ASP。. NET, MVC5, EF6
每个应用程序用户可以有多个tsaccountcredentials
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上下文迁移,该表已经存在。
第二,当我使用tsaccountcredentials上下文添加迁移时,我得到以下错误: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合并了
为什么要分离DB?我认为你想在相同的数据库中创建2个表,而不是不同的数据库,因此删除TfsAccountCredentialsDb
并将TfsAccountCredentials
添加到IdentityDb
:
public class IdentityDb : IdentityDbContext<ApplicationUser>
{
public IdentityDb() : base("DefaultConnection")
{
}
public DbSet<TfsAccountCredentials> TfsAccountCredentials { get; set; }
}
您不需要IdentityDb
,因为IdentityDbContext<ApplicationUser>
是处理身份问题的上下文。那么你可以:
A) TfsAccountCredentialsDb
继承IdentityDbContext<ApplicationUser>
。这将允许您在您的模型中引用ApplicationUser。ApplicationUser的DbSet在基类中,你看不到。
B)如果你想要TfsAccountCredentialsDb
从DbContext
继承,那么你将不得不在该上下文中添加ApplicationUser
的副本以及它的DbSet
,你可以使用它来引用你的应用程序的上下文中。