我有一个现有的DbContext类,我想将其用作其他Web项目的基类。 DbContext包含一个真正完整的表结构,用于注册公司和人员以及相关信息。
开始一个新项目时,我不想一次又一次地创建相同的结构!所以我尝试重用现有的DbContext作为我的新项目的基类,只需添加新表来使其在新应用程序中按需运行。
但是,我找不到让它运转的方法! DbContext基类如下所示,它来自另一个项目:
public class TemplateDbContext
{
private readonly Guid _instanceId;
bool _disposed;
public Guid InstanceId { get { return _instanceId; } }
public TemplateDbContext ()
: base("name=" + GetConnectionStringName)
{
}
public static TemplateDbContext Create()
{
return new TemplateDbContext ();
}
private void SetOneToMany(DbModelBuilder modelBuilder)
{
}
private void SetManyToMany(DbModelBuilder modelBuilder)
{
}
private void SetOneToOne(DbModelBuilder modelBuilder)
{
}
private void SetOneNavigation(DbModelBuilder modelBuilder)
{
}
private void SetKeys(DbModelBuilder modelBuilder)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUserClaim>().ToTable("MembershipClaims");
modelBuilder.Entity<IdentityUserRole>().ToTable("MembershipRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("MembershipLogins");
modelBuilder.Entity<IdentityRole>().ToTable("Roles");
modelBuilder.Entity<Membership>().ToTable("Memberships");
SetKeys(modelBuilder);
SetOneNavigation(modelBuilder);
SetOneToOne(modelBuilder);
SetOneToMany(modelBuilder);
SetManyToMany(modelBuilder);
}
public static string GetConnectionStringName
{
get
{
...
}
}
public DbSet<UserProfile> UserProfiles
{
get;
set;
}
// I have many more DbSet defined here...
public override int SaveChanges()
{
...
}
public override async Task<int> SaveChangesAsync()
{
...
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken)
{
...
}
public void SyncObjectState<TEntity>(TEntity entity) where TEntity : class, IObjectState
{
...
}
private void SyncObjectsStatePreCommit()
{
...
}
public void SyncObjectsStatePostCommit()
{
...
}
protected override void Dispose(bool disposing)
{
...
}
}
这是我的新项目的ApplicationDbContext:
public class ApplicationDbContext : TemplateDbContext
{
public DbSet<Fruit> Fruits { get; set; }
// I have many more DbSet define here...
private void SetOneToMany(DbModelBuilder modelBuilder)
{
...
}
private void SetManyToMany(DbModelBuilder modelBuilder)
{
...
}
private void SetOneToOne(DbModelBuilder modelBuilder)
{
...
}
private void SetOneNavigation(DbModelBuilder modelBuilder)
{
...
}
private void SetKeys(DbModelBuilder modelBuilder)
{
...
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
SetKeys(modelBuilder);
SetOneNavigation(modelBuilder);
SetOneToOne(modelBuilder);
SetOneToMany(modelBuilder);
SetManyToMany(modelBuilder);
}
}
你可以做客,但这根本不起作用,我不知道如何才能达到我想要的效果。我甚至不知道是否可以不复制TemplateDbContext类的所有代码。 我现在完成的最好的事情,它只生成TemplateDbContext,通过在新项目中执行Add Manager Migration with Package Manager Console。因此迁移错过了定义到ApplicationDbContext类中的所有DbSet。
我需要做什么样的结构?
答案 0 :(得分:1)
您可以在Context
指定enable migration
的名称,并以相同的方式使用其他命令,您可以找到更多信息about this here。
enable-migrations -ContextTypeName <DbContext-Name-with-Namespaces> -MigrationsDirectory:<Migrations-Directory-Name>
Add-Migration -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> <Migrations-Name>
Update-Database -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> -Verbose
注意:不要忘记将默认项目更改为程序包管理器控制台中的ApplicationDbContext
。< / p>
另外我认为您在base.OnModelCreating(modelBuilder)
覆盖方法时忘记拨打ApplicationDbContext
。