[英]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-Migration Inherit_FirstDbContext
) Add-Migration Inherit_FirstDbContext
) Up
and Down
methods Up
和Down
方法中删除所有内容 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.