entity-framework-4 - Entity Framework 4 - 在持久性未知上下文中使用 CTP5(代码优先)映射非公共(public)属性

标签 entity-framework-4 ef-code-first code-first entity-framework-4.1 entity-framework-ctp5

我知道这个问题已经有了解决方案(例如 this question ),但我真的无法将映射逻辑附加到域(POCO 类)所在的同一程序集中。

还有其他方法吗?

我找到了 nice blog post但我无法让它工作。
这是模型:

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

映射代码:
var instituteTextExpression = ObjectAccessor<Institute>.CreateExpression<ICollection<InstituteText>>("InnerInstituteTexts");

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

其中 CreateExpression 定义为:
public static Expression<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;
}

我得到的错误是:

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..

最佳答案

首先想到的是 InternalsVisibleTo 程序集属性。这允许您命名也可以访问内部成员的“ friend ”程序集。我不确定这是否适用于 EF CodeFirst,但我试过了,它似乎工作得很好。您将不得不稍微更改您的模型组件,但我认为这是一个合理的更改。

您首先需要将您的属性声明更改为 protected 内部:

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

然后,在您的模型装配体中,添加 InternalsVisibleTo AssemblyInfo.cs 文件中带有映射程序集名称的程序集属性。
[assembly: InternalsVisibleTo("MappingAssemblyName")]

最后,您可以在映射程序集中定义属性的映射,就像任何其他可公开访问的属性一样。
institute.HasMany(i => i.InnerInstituteTexts)
    .WithRequired()
    .HasForeignKey(t => t.InstituteId);

关于entity-framework-4 - Entity Framework 4 - 在持久性未知上下文中使用 CTP5(代码优先)映射非公共(public)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5246466/

相关文章:

entity-framework - EF代码优先循环引用

c# - 覆盖 SaveChanges 并设置 ModifiedDate,但如何设置 ModifiedBy?

c# - 如何在 Entity Framework Code First 中建立多对多关联

c# - 影响 EF 代码中的外键列命名(CTP5)

c# - Entity Framework 4.1 - 如何将 "Force"EF 转至 DB 而不是使用 Graph?

entity-framework - 解决 EF4 中的实体与我们的数据库标准之间的命名约定冲突?

.net - EF4 无法将具体类型转换为接口(interface)

c# - Entity Framework : Dependencies due to foreign key constraints

java - 我如何使用 hibernate 生成迁移?

wcf - EDM -> POCO -> WCF (.NET4) 但是传输集合会导致 IsReadOnly 设置为 TRUE