WithOptional with Entity Framework Core

感情迁移 提交于 2019-12-07 03:46:28

问题


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.

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

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


回答1:


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);

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



来源:https://stackoverflow.com/questions/38513232/withoptional-with-entity-framework-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!