导航 属性 不退出

Navigation property doesn't exit

我正在使用 C# 和 .NET Framework 4.5.1 开发 Entity Framework 6.1.2 Code First 库。

我收到这个错误:

A specified Include path is not valid. The EntityType 'MyProject.Data.SqlServer.Concrete.AGGREGATION_CHILDS' does not declare a navigation property with the name 'Code'.

请注意,MyProject.Data.SqlServer.Concrete 命名空间不正确。我在 MyProject.Data.SqlServer.Concrete 命名空间中声明了 DbContext

这是AGGREGATION_CHILDSclass声明:

namespace MyProject.Data
{
    public class AGGREGATION_CHILDS
    {
        public string CODE { get; set; }
        public string PARENT_CODE { get; set; }
        public int POSITION { get; set; }

        public virtual AGGREGATIONS Aggregation { get; set; }
        public virtual CODES Code { get; set; }
    }
}

CODES class:

namespace MyProject.Data
{
    public class CODES
    {
        public string CODE { get; set; }
        public byte CODE_LEVEL { get; set; }
        [ ... ]

        public virtual AGGREGATION_CHILDS AggregationChild { get; set; }
    }
}

及其配置文件:

namespace MyProject.Data.SqlServer.Configurations
{
    class AGGREGATION_CHILDSConfiguration : EntityTypeConfiguration<AGGREGATION_CHILDS>
    {
        public AGGREGATION_CHILDSConfiguration()
        {
            HasKey(ag_ch => ag_ch.CODE);

            Property(ag_ch => ag_ch.CODE)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            Property(ag_ch => ag_ch.CODE)
                .HasMaxLength(20)
                .IsRequired();

            Property(ag_ch => ag_ch.PARENT_CODE)
                .HasMaxLength(20)
                .IsRequired();

            HasRequired(ag_ch => ag_ch.Aggregation)
                .WithMany(ag => ag.AggregationChilds)
                .HasForeignKey(ag_ch => ag_ch.PARENT_CODE);

            HasRequired(ag_ch => ag_ch.Code)
                .WithOptional(c => c.AggregationChild)
                .WillCascadeOnDelete(false);
        }
    }
}

和另一个配置文件:

namespace MyProject.Data.SqlServer.Configurations
{
    class CODESConfiguration : EntityTypeConfiguration<CODES>
    {
        public CODESConfiguration()
        {
            HasKey(c => c.CODE);

            Property(c => c.CODE)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            Property(c => c.CODE)
                .HasMaxLength(20)
                .IsRequired();

            [ ... ]
        }
    }
}

这就是我收到错误的地方:

List<AGGREGATION_CHILDS> agChilds =
                m_AggChildRepo
                .SearchForWithInclude(agCH => agCH.PARENT_CODE == aggregation.PARENT_CODE, "Code")
                .ToList<AGGREGATION_CHILDS>();

SearchForWithInclude 实现是:

public IQueryable<TEntity> SearchForWithInclude(
    Expression<Func<TEntity, bool>> predicate,
    string includePath)
{
    return _dbSet.Where(predicate).Include(includePath);
}

CODESAGGREGATION_CHILDS 具有一对零或一的关系。

你知道为什么抱怨代码导航 属性 不退出吗?也许,我没有正确创建零或一对一关系。

触发错误的行计算为

m_AggChildRepo.Where(agCH => agCH.PARENT_CODE == aggregation.PARENT_CODE).Include("Code")

include 应该做什么?即使您使用的是 Dynamic Linq,"Code" 应该是什么?您在此处发布的所有片段都使用完全大写的属性和 属性 名称。

这就是我解决问题的方法。我认为有一个模棱两可的问题。

namespace MyProject.Data
{
    public class AGGREGATION_CHILDS
    {
        public string CHILD_CODE { get; set; }
        public string PARENT_CODE { get; set; }
        public int POSITION { get; set; }

        public virtual AGGREGATIONS Aggregation { get; set; }
        public virtual CODES Code { get; set; }
    }
}

我已将 AGGREGATION_CHILDS.CODE 属性 更改为 AGGREGATION_CHILDS.CHILD_CODE

namespace MyProject.Data.SqlServer.Configurations
{
    class AGGREGATION_CHILDSConfiguration : EntityTypeConfiguration<AGGREGATION_CHILDS>
    {
        public AGGREGATION_CHILDSConfiguration()
        {
            HasKey(ag_ch => ag_ch.CHILD_CODE);

            Property(ag_ch => ag_ch.CHILD_CODE)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            Property(ag_ch => ag_ch.CHILD_CODE)
                .HasMaxLength(20)
                .IsRequired()
                .HasColumnName("CODE");

            Property(ag_ch => ag_ch.PARENT_CODE)
                .HasMaxLength(20)
                .IsRequired();

            HasRequired(ag_ch => ag_ch.Aggregation)
                .WithMany(ag => ag.AggregationChilds)
                .HasForeignKey(ag_ch => ag_ch.PARENT_CODE);

            HasRequired(ag_ch => ag_ch.Code)
                .WithOptional(c => c.AggregationChild)
                .WillCascadeOnDelete(false);
        }
    }
}

并设置数据库中的列名:

Property(ag_ch => ag_ch.CHILD_CODE)
    .HasMaxLength(20)
    .IsRequired()
    .HasColumnName("CODE");