WithOptional 与 Entity Framework Core [英] WithOptional with Entity Framework Core

查看:33
本文介绍了WithOptional 与 Entity Framework Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的旧应用迁移到新的 EF Core,但我找不到一些关系,例如:

I'm trying to migrate my old app to the new EF Core but I cannot find some relationships like:

  HasRequired(o => o.Document).WithOptional(o => o.CancelNote);

有一些扩展方法吗?我在文档上找不到.

Is there some extension methods? I cannot find on the docs.

HasRequired 我认为可以用 HasOne() 方法代替,但是 WithOptional() 怎么样?

The HasRequired I think that is possible to substitute with HasOne() method, but how about the WithOptional()?

其他事情,根据 docs 实体不使用virtual 关键字来创建导航属性,延迟加载如何工作?

Other thing, according to the docs the entity not uses the virtual keyword to create the navigation properties, how lazy load will work?

推荐答案

您不会在 EF7 中找到 HasOptional 等效方法.按照惯例,如果您的 FK 属性可为空,您的导航属性将被视为可选

You will not find an HasOptional equivalent method in EF7. By convention if your FK property is nullable, you navigation property will be treated as optional

 modelBuilder.Entity<Blog>()
                .HasOne(p => p.Document)
                .WithOne(i => i.CancelNote)
                .HasForeignKey<Document>(b => b.CancelNoteForeignKey);

关于您的第二个问题,EF Core (EF7) 不支持延迟加载.在此链接中,您将找到现在要加载的选项相关实体

About your second question,EF Core (EF7) doesn't support Lazy Loading. In this link you will find the options you have now to load related entities

这篇关于WithOptional 与 Entity Framework Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆