[英]Convert WithOptional to Entity Framework Core(7) equivalent
Im migrating a project from.Net 4.X to.Net 6 and EF 6 to the latest version (version 7 i believe) using Visual Studio 2022.我正在使用 Visual Studio 2022 将项目从 .Net 4.X 迁移到 .Net 6 和 EF 6 到最新版本(我相信是版本 7)。
I've migrated a bunch of configurations but the below im not sure the best way to proceed (the database already exists)我已经迁移了一堆配置,但下面我不确定继续的最佳方式(数据库已经存在)
Here is EF6 code这是 EF6 代码
internal class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
public CustomerConfiguration()
{
this.HasMany(e => e.CustomerDocuments)
.WithOptional(e => e.Customer)
.HasForeignKey(e => e.CustomerID);
}
}
In EF 7 i have the code as在 EF 7 中,我的代码为
internal class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.HasMany(e => e.CustomerDocuments)
}
}
But i cant find the equivalent for .WithOptional
and https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key doesnt really show me any example of how i can configure it although .HasForeignKey
seems to exist but i think once WithOptional
is resolved it may give some way to convert/use HasForeignKey
.但我找不到.WithOptional
和https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key 的等价物确实向我展示了如何配置它的任何示例,尽管.HasForeignKey
似乎存在,但我认为一旦WithOptional
得到解决,它可能会提供一些转换/使用HasForeignKey
的方法。
I read WithOptional with Entity Framework Core but then i get confused with if its replacement is HasOne
as im already using WithOne
(in another Entity configuration) to convert WithRequired
(from EF 6)我阅读了带有 Entity Framework Core 的 WithOptional,但后来我对它的替代品是否为HasOne
感到困惑,因为我已经在使用WithOne
(在另一个实体配置中)来转换WithRequired
(来自 EF 6)
Anyone know what im missing here or how to convert to the equivalent in EF 7?任何人都知道我在这里缺少什么或如何转换为 EF 7 中的等效项?
In EF Core these are simply separated to WithOne
(for relationship cardinality and associated reference navigation property mapping) and IsRequired
(whether it required/optional).在 EF Core 中,它们简单地分为WithOne
(用于关系基数和关联的引用导航属性映射)和IsRequired
(无论它是必需的还是可选的)。
So the general conversion of EF6 WithOptional
/ WithRequired
after HasMany
/ HasOne
to EF Core is like所以在HasMany
/ WithRequired
之后WithOptional
/ HasOne
到 EF Core 的一般转换就像
.WithOptional(e => e.Customer)
maps to映射到
.WithOne(e => e.Customer)
.IsRequired(false)
and和
.WithRequired(e => e.Customer)
maps to映射到
.WithOne(e => e.Customer)
.IsRequired(true) // or just .IsRequired()
The same applies if you start configuration from the "one" side, ie HasOptional
/ HasRequired
become HasOne().With{One|Many}).IsRequired(false|true)
如果您从“一”端开始配置,这同样适用,即HasOptional
/ HasRequired
变为HasOne().With{One|Many}).IsRequired(false|true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.