与 applicationuser 一对多
One to many with applicationuser
如果这是一个 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 table,由于 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 合并了。
为什么要分离数据库?我想你想在 same DB 而不是不同的 DB 中制作 2 table 所以删除你的 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 在您看不到的基础 class 中。
B) 如果您希望 TfsAccountCredentialsDb
继承自 DbContext
,那么您必须在该上下文中添加 ApplicationUser
的副本以及 DbSet
您可以在应用的上下文中使用引用。
如果这是一个 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 table,由于 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 合并了。
为什么要分离数据库?我想你想在 same DB 而不是不同的 DB 中制作 2 table 所以删除你的 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 在您看不到的基础 class 中。
B) 如果您希望 TfsAccountCredentialsDb
继承自 DbContext
,那么您必须在该上下文中添加 ApplicationUser
的副本以及 DbSet
您可以在应用的上下文中使用引用。