开发者

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

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.

Is there any other way?

I found this nice blog post but I couldn't get it working. Here is the model:

public class Institute
{
    /**
        Code omitted
    **/

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

    private InstituteTextSet _TextSets;

    public InstituteTextSet Texts 
    {
        get 
        {
            if (_TextSets == null)
                _TextSets = new InstituteTextSet(InnerInstituteTexts);

            return _TextSets;
        }
    }
}

Mapping code:

var instituteTextExpression = ObjectAccessor<Institute>.CreateExpression<ICollection<InstituteText>>("InnerInstituteTexts");

institute.HasMany(instituteTextExpression)
    .WithRequired()
    .HasForeignKey(t => t.InstituteId);

where CreateExpression is defined as:

public static E开发者_运维技巧xpression<Func<T, TResult>> CreateExpression<TResult>(string propertyOrFieldName)
{
    ParameterExpression param = Expression.Parameter(typeof(T), "propertyOrFieldContainer");
    Expression body = Expression.PropertyOrField(param, propertyOrFieldName);
    LambdaExpression lambda = Expression.Lambda(typeof(Func<T, TResult>), body, param);

    return (Expression<Func<T, TResult>>) lambda;
}

the error I get is:

Initialization method Studentum.Core.Tests.InstituteTests.Initialize threw exception. System.TypeInitializationException: System.TypeInitializationException: The type initializer for 'Studentum.Core.FluentCoreRepositoryFactory' threw an exception. ---> System.InvalidOperationException: The configured property 'InnerInstituteTexts' is not a declared property on the entity 'Institute'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property..


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜