[英]To add navigation property without foreign key in EF Core, DB-first migration with .NET Core Web API
I am working with an existing system and updating it to .NET Core, Web API and EF Core.我正在使用现有系统并将其更新到 .NET Core、Web API 和 EF Core。
The existing system has 2 tables:现有系统有 2 个表:
Though ParentId
exists in the child table, there is no foreign key reference, but I want to be able to use include when I query the parent.虽然ParentId
存在于子表中,但没有外键引用,但我希望在查询父表时能够使用 include。 I have asked not to add FK as part of deleting they are putting -ve values to parentId
column.我已经要求不要添加 FK 作为删除它们将 -ve 值放入parentId
列的一部分。 This way they can bring it back and a legacy system was built that way.通过这种方式,他们可以将其带回来,并以这种方式构建遗留系统。
Now, in db-first migration how can I specify a navigation property without fk so my EF Core to act relational;现在,在 db-first 迁移中,如何在没有 fk 的情况下指定导航属性,以便我的 EF Core 执行关系; or at least return them together.或者至少将它们一起归还。 Adding nullable foreign key is not an option as it will break the system when -ve values are added.添加可为空的外键不是一个选项,因为当添加 -ve 值时它会破坏系统。
I do have suggested for full cleanup of DB and getting rid of -ve values but that involves lots of testing and no deliverable.我确实建议完全清理数据库并摆脱 -ve 值,但这涉及大量测试并且没有可交付成果。 So long story short how to add navigation property without foreign key in database first migration?长话短说,如何在数据库首次迁移中添加没有外键的导航属性?
I tried adding collection and virtual entry in the model, but after migration it got overwritten.我尝试在 model 中添加集合和虚拟条目,但迁移后它被覆盖了。 I have added this by using HasMany on modelbuilder as per this document - https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key我已经按照本文档在模型构建器上使用 HasMany 添加了这个 - https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key% 2C简单键
But scaffolding is overriding my navigation property但是脚手架覆盖了我的导航属性
I found out the answer for this.我找到了答案。
In EF core 3.x the dbcontext created by DBFrist scaffolding is all partial classes.在 EF 核心 3.x 中,由 DBFrist 脚手架创建的 dbcontext 都是部分类。 So I did the following: 1. new partial class for context class - here i added the relationship of navigation property using OnModelCreatingPartial() method.所以我做了以下事情: 1. 上下文 class 的新部分 class - 这里我使用 OnModelCreatingPartial() 方法添加了导航属性的关系。 Example below下面的例子
public partial class dbContext : DbContext
{
partial void OnModelCreatingPartial(ModelBuilder builder)
{
builder.Entity<Packcomponent>()
.HasOne(p => p.Pack)
.WithMany(b => b.PackComponent)
.HasForeignKey(p => p.PackId);
}
}
extended the partial class in a new file and added navigation property there.在新文件中扩展了部分 class 并在那里添加了导航属性。
public partial class Packcomponent { public Pack Pack { get; set; } } public partial class Pack { public List PackComponent { get; set; } }
This way upon scaffolding it did not overwrite custom navigation properties and I also could use this properties to do EF operations like.Include() and to save related entities as well.这样在搭建脚手架时它不会覆盖自定义导航属性,我也可以使用此属性来执行 EF 操作,例如.Include() 并保存相关实体。 It is pretty awesome!!真是太棒了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.