简体   繁体   English

DbContext继承更新 - 数据库错误

[英]DbContext inheritance update-database error

We are using EntityFramework code-first I want to implement DbContext inheritance as: 我们正在使用EntityFramework代码 - 首先我要实现DbContext继承:

public class AgileDbContextBase : DbContext
{
    public DbSet<Account> Accounts { get; set; }
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // base.OnModelCreating(modelBuilder);
        // TODO configure relations here!
        modelBuilder.Configurations.Add(new AccountEntityConfiguration())
            .Add(new UserEntityConfiguration());
    }
}

and

public class LoadContext : AgileDbContextBase
{
    #region Public Properties
    public DbSet<ProjectLoadEstimate> ProjectLoadEstimates { get; set; }
    public DbSet<OccupationType> OccupationTypes { get; set; }
    public DbSet<LoadReport> LoadReports { get; set; } 
    #endregion

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new ProjectLoadEstimateConfiguration())
            .Add(new OccupationTypeEntityConfiguration())
            .Add(new LoadReportEntityConfiguration());
    }
}

one of the entities uses another one which is in AgileDbContextBase context, here is it: 其中一个实体使用AgileDbContextBase上下文中的另一个实体,这里是:

public class LoadReport : BaseEntity
{
    /// <summary>
    ///     The date of load report has been created
    /// </summary>
    public virtual DateTime Created { get; set; }

    /// <summary>
    ///     The project manager
    /// </summary>
    public User ProjectManager { get; set; }

    /// <summary>
    ///     The Id of project manager
    /// </summary>
    public int ProjectManagerId { get; set; }
}

both contexts are using automatic migrations. 两个上下文都在使用自动迁移。 The problem is that when I try to update-database on LoadContext PackageManager says that 问题是,当我尝试在LoadContext上更新数据库时,PackageManager说

There is already an object named 'Accounts' in the database. 数据库中已有一个名为“Accounts”的对象。

It Looks like that it tries to re-create table which already exists. 看起来它试图重新创建已经存在的表。 How to avoid that? 怎么避免呢? How to tell code-first that from LoadContext it MUST create ONLY tables in Database which are defined as DbSet<> in LoadContext ? 如何告诉代码优先从LoadContext它必须在数据库中创建ONLY表,在LoadContext中定义为DbSet <>? (maybe some workaround)? (也许是一些解决方法)? PS Both contexts use the same connection string. PS两个上下文都使用相同的连接字符串。

Thank you for your time! 感谢您的时间!

Override the OnModelCreating method and configure the model builder to ignore entities you want. 重写OnModelCreating方法并配置模型构建器以忽略所需的实体。

Check this blog post for more details http://msdn.microsoft.com/en-us/data/jj591617.aspx 查看此博客文章了解更多详情http://msdn.microsoft.com/en-us/data/jj591617.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM