ASP MVC 中的多个 DbContext:可以避免 IdentityDbContext 继承吗?
Multiple DbContext in ASP MVC: Possible to avoid IdentityDbContext inheritance?
我正在使用 EntityFramework 6. 我想将我的 DbContext 拆分成多个较小的。编辑:我有一个要连接的数据库。
我将拥有一个 DbContext,其中包含只有一个中央应用程序会使用的所有实体和迁移。其他应用程序将使用较小的 DbContext 而无需迁移。
我发现我需要从 IdentityDbContext 继承所有 DbContext,即使他们使用了它的 none。
internal class SmallerDbContext : IdentityDbContext<ApplicationUser,
ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public DbContext<Entity1> NotInSmallerDbContext { get; set; }
public DbContext<Entity1> InSmallerDbContext { get; set; }
}
我希望这样:
internal class SmallerDbContext : System.Data.Entity.DbContext
{
public DbContext<Entity1> InSmallerDbContext { get; set; }
}
但这会产生以下错误:
Paris.DAL.Repo.DbContext.ApplicationUserLogin: : EntityType 'ApplicationUserLogin' has no key defined. Define the key for this EntityType.
Paris.DAL.Repo.DbContext.ApplicationUserRole: : EntityType 'ApplicationUserRole' has no key defined. Define the key for this EntityType.
ApplicationUserLogins: EntityType: EntitySet 'ApplicationUserLogins' is based on type 'ApplicationUserLogin' that has no keys defined.
ApplicationUserRoles: EntityType: EntitySet 'ApplicationUserRoles' is based on type 'ApplicationUserRole' that has no keys defined.
我尝试将这些表添加到 DbContext,但无济于事。
public IDbSet<ApplicationUser> Users { get; set; }
public IDbSet<ApplicationRole> Roles { get; set; }
有什么方法可以创建不从 IdentityDbContext 继承的 DbContext 吗?
通过将对象添加到 DbContext,我走在了正确的道路上。
您只需添加两个,User 和 UserLogin。其他的不需要。
public DbSet<ApplicationUser> Users { get; set; }
public DbSet<ApplicationUserLogin> UserLogins { get; set; }
此外,您需要为这两个对象设置正确的 table,并且您需要添加一些虚拟属性,以便 Entity Framework 可以像处理其他任何类型一样处理这些类型 类.
[Table("AspNetUsers")]
public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
}
[Table("AspNetUserLogins")]
public class ApplicationUserLogin : IdentityUserLogin<int>
{
[Key, Column(Order = 0)]
public override string LoginProvider { get; set; }
[Key, Column(Order = 1)]
public override string ProviderKey { get; set; }
[Key, Column(Order = 2)]
public override int UserId { get; set; }
}
现在,您的 DbContext 不再需要继承自 IdentityDbContext。
我正在使用 EntityFramework 6. 我想将我的 DbContext 拆分成多个较小的。编辑:我有一个要连接的数据库。
我将拥有一个 DbContext,其中包含只有一个中央应用程序会使用的所有实体和迁移。其他应用程序将使用较小的 DbContext 而无需迁移。
我发现我需要从 IdentityDbContext 继承所有 DbContext,即使他们使用了它的 none。
internal class SmallerDbContext : IdentityDbContext<ApplicationUser,
ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public DbContext<Entity1> NotInSmallerDbContext { get; set; }
public DbContext<Entity1> InSmallerDbContext { get; set; }
}
我希望这样:
internal class SmallerDbContext : System.Data.Entity.DbContext
{
public DbContext<Entity1> InSmallerDbContext { get; set; }
}
但这会产生以下错误:
Paris.DAL.Repo.DbContext.ApplicationUserLogin: : EntityType 'ApplicationUserLogin' has no key defined. Define the key for this EntityType.
Paris.DAL.Repo.DbContext.ApplicationUserRole: : EntityType 'ApplicationUserRole' has no key defined. Define the key for this EntityType.
ApplicationUserLogins: EntityType: EntitySet 'ApplicationUserLogins' is based on type 'ApplicationUserLogin' that has no keys defined.
ApplicationUserRoles: EntityType: EntitySet 'ApplicationUserRoles' is based on type 'ApplicationUserRole' that has no keys defined.
我尝试将这些表添加到 DbContext,但无济于事。
public IDbSet<ApplicationUser> Users { get; set; }
public IDbSet<ApplicationRole> Roles { get; set; }
有什么方法可以创建不从 IdentityDbContext 继承的 DbContext 吗?
通过将对象添加到 DbContext,我走在了正确的道路上。 您只需添加两个,User 和 UserLogin。其他的不需要。
public DbSet<ApplicationUser> Users { get; set; }
public DbSet<ApplicationUserLogin> UserLogins { get; set; }
此外,您需要为这两个对象设置正确的 table,并且您需要添加一些虚拟属性,以便 Entity Framework 可以像处理其他任何类型一样处理这些类型 类.
[Table("AspNetUsers")]
public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
}
[Table("AspNetUserLogins")]
public class ApplicationUserLogin : IdentityUserLogin<int>
{
[Key, Column(Order = 0)]
public override string LoginProvider { get; set; }
[Key, Column(Order = 1)]
public override string ProviderKey { get; set; }
[Key, Column(Order = 2)]
public override int UserId { get; set; }
}
现在,您的 DbContext 不再需要继承自 IdentityDbContext。