简体   繁体   English

两个上下文之间的实体框架核心导航属性

[英]Entity Framework core navigation property between two contexts

There are similar questions with this issue, but not for EF Core. 这个问题也有类似的问题,但EF Core则没有。 Not a duplicate of Entity Framework Core Using multiple DbContexts . 不能使用多个DbContext复制Entity Framework Core That one is related to not being able to access the database at all from the second context, and the two contexts use different databases. 这与根本无法从第二个上下文访问数据库有关,并且两个上下文使用不同的数据库。 This question is about a single database and the issue is related to migrations 这个问题是关于单个数据库的,并且这个问题与迁移有关


I have two EF core db contexts using the same SQL Server database. 我有两个使用同一SQL Server数据库的EF核心db上下文。

In the first context I have many entities, one of them is User. 在第一个上下文中,我有很多实体,其中一个是User。

In the second one there is a single entity called UserExt which has a navigational property to User 在第二个中,有一个称为UserExt的实体,它具有对User的导航属性

public class UserExt
{
    [Key]
    public long UserID { get; set; }
    public virtual User User { get; set; }

    [Required]
    public string Address { get; set; }
}

The issue is that when creating the migration for UserExt using 'add-migration', all entities from the first context are also included. 问题是,当使用“ add-migration”为UserExt创建迁移时,还包括第一个上下文中的所有实体。

Tried providing the context, but same result 尝试提供上下文,但结果相同

add-migration --context SecondContext

With EF 6 it was possible to solve this using ContextKey ( https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrationsconfiguration.contextkey(v=vs.113).aspx ) but has not been ported to EF Core 使用EF 6可以使用ContextKey( https://msdn.microsoft.com/zh-cn/library/system.data.entity.migrations.dbmigrationsconfiguration.contextkey ( v= vs.113).aspx)解决此问题尚未移植到EF Core

Is there a way to make this work so that the migrations in the second context would contain only its entities ? 有没有办法使这项工作有效,以便第二个上下文中的迁移仅包含其实体?

Solved using database context inheritance. 使用数据库上下文继承解决。 This way I can have separate migrations. 这样,我可以进行单独的迁移。

public class SecondDbContext : FirstDbContext
{
    public virtual DbSet<UserExt> ExtendedUsers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
       optionsBuilder.UseSqlServer("connectionString", options => 
            options
               .MigrationsAssembly("SecondDbContextAssemblyName")
               .MigrationsHistoryTable("__SecondEFMigrationsHistory") // separate table to store migration history to avoid conflicts
       );
    } 
}

Because SecondDbContext is inherited from FirstDbContext , every entity change made in the base context will be inherited. 因为SecondDbContext是从FirstDbContext继承的, FirstDbContext在基本上下文中进行的每个实体更改都将被继承。 Which means that when a new migration is added to the second context, it will try to apply the changes again from the first context again. 这意味着将新迁移添加到第二个上下文时,它将尝试再次应用第一个上下文中的更改。 Workaround is to: 解决方法是:

  • Add a new migration (for example Add-Migration Inherit_FirstDbContext ) 添加一个新的迁移(例如Add-Migration Inherit_FirstDbContext
  • Delete everything from the migration's Up and Down methods 从迁移的UpDown方法中删除所有内容
  • Apply the empty migration in the database. 在数据库中应用空迁移。 ( Update-Database ) Update-Database

This ensures that the Entity Framework snapshot will contain the changes, without actually having to re-apply them in the database. 这确保了Entity Framework快照将包含更改,而无需实际将它们重新应用到数据库中。

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

相关问题 实体框架代码优先,不同上下文/数据库之间的导航属性 - Entity Framework Code First, Navigation Property between different contexts/databases 实体框架中两个上下文之间的 Inheritance - Inheritance between two contexts in Entity Framework 如何使用Entity Framework在MVC中的两个上下文之间传递数据 - How to pass data between two contexts in MVC with Entity Framework Entity Framework Core 在导航属性上调用表达式 - Entity Framework Core Invoke an Expression on Navigation property Entity Framework Core:基于导航属性的集合 - Entity Framework Core: navigation property based collection Entity Framework Core:填充关系导航属性 - Entity Framework Core : populating relationship navigation property Entity Framework Core 5 - 单一导航属性 - Entity Framework Core 5 - Single Navigation Property 实体框架导航属性 - Entity Framework navigation property 两个嵌套的Entity Framework上下文,共享一个事务 - Two nested Entity Framework contexts, sharing a transaction 获取简单属性和导航属性之间的映射-Entity Framework 5 - Get mapping between simple property and navigation property - Entity Framework 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM