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

前端 未结 1 1976
自闭症患者
自闭症患者 2021-02-13 18:30

I know the question already has a solution (eg. this question) but I really can\'t afford to attach the mapping logic in the same assembly where the domain (POCO classes) is.

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-13 19:01

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

    0 讨论(0)
提交回复
热议问题