Entity Framework 4 - Mapping non-public properties with CTP5 (code first) in a persistence unaware context

£可爱£侵袭症+ 提交于 2019-12-03 11:19:39

The first thing that came to mind is the InternalsVisibleTo assembly attribute. This allows you to name "friend" assemblies that also have access to internal members. I wasn't sure if this would work with EF CodeFirst, but I tried it and it appears to work just fine. You would have to change your model assembly slightly, but I think it is a reasonable change.

You would first need to change your property declaration to protected internal:

 protected internal virtual ICollection<InstituteText> InnerInstituteTexts { get; set; }

Then, in your model assembly, add the InternalsVisibleTo assembly attribute in your AssemblyInfo.cs file with the name of your mapping assembly.

[assembly: InternalsVisibleTo("MappingAssemblyName")]

Finally, you can define your mapping of the property in the mapping assembly like any other publicly accessible property.

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