与Entity Framework Core一起使用 [英] WithOptional with Entity Framework Core

查看:110
本文介绍了与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.

有必要我认为可以用 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

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

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