简体   繁体   English

将 WithOptional 转换为等效的 Entity Framework Core(7)

[英]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 .但我找不到.WithOptionalhttps://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.

相关问题 Oracle LPad 等效于实体框架核心 - Oracle LPad equivalent in entity framework core 实体框架核心中等效的 SQL RIGHT 函数 - SQL RIGHT function equivalent in Entity framework Core 如何使用 Entity Framework Core 中的 Join() 方法将 Sql 查询转换为 Linq 及其等效项 - How can I convert Sql query to Linq and its equivalent with Join() method in Entity Framework Core 将实体框架核心实体转换为SQL字符串 - Convert Entity Framework Core entity into SQL string Entity Framework Core 2.2.4中System.Data.Entity的等价物是什么? - What is the equivalent of System.Data.Entity in Entity Framework Core 2.2.4? 在实体框架核心中将字典转换为ICollection - Convert Dictionary to ICollection in Entity Framework Core EF Core Concurrency,相当于Entity Framework 中的SQL Addition (+=)? - EF Core Concurrency, what is the equivalent of SQL Addition (+=) in Entity Framework? 什么是等效于 modelBuilder 的 HasDefaultValueSql 的 Entity Framework Core 属性? - What is the Entity Framework Core attribute equivalent to modelBuilder's HasDefaultValueSql? Entity Framework等效于InsertonSubmit - Entity Framework equivalent of InsertonSubmit HasPrincipalKey() 等同于 Entity Framework 6? - HasPrincipalKey() Equivalent for Entity Framework 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM