这是我的实施。
public partial class Person
{
#region Constructors
public Person()
{
PersonExpirableCredentials = new List<personexpirablecredential>();
}
#endregion Constructors
#region Properties
public int PersonID
{
get;
protected set;
}
public virtual ICollection<personexpirablecredential> PersonExpirableCredentials
{
get;
set;
}
#endregion
}
public abstract class PersonCredential
{
#region Constructors
public PersonCredential()
{
}
#endregion
#region Properties
public int PersonID
{
get;
protected set;
}
public int PersonCredentialID
{
get;
protected set;
}
public System.DateTime WhenEffective
{
get;
set;
}
public Nullable<system.datetime> WhenExpire
{
get;
set;
}
public virtual Person Person
{
get;
set;
}
}
public partial class PersonExpirableCredential : PersonCredential
{
#region Constructors
public PersonExpirableCredential() :
base()
{
}
#endregion
#region Properties
public DateTime? WhenCompleted
{
get;
set;
}
public string CredentialNumber
{
get;
set;
}
#endregion
}
以下是流畅的映射
internal partial class PersonMapping : EntityTypeConfiguration<person>
{
#region Constructors
public PersonMapping()
{
this.HasKey(t => t.PersonID);
this.ToTable("Person");
}
#endregion
}
internal partial class PersonCredentialMapping : EntityTypeConfiguration<personcredential>
{
#region Constructors
public PersonCredentialMapping()
{
this.HasKey(t => new { t.PersonCredentialID });
this.ToTable("PersonCredential");
this.HasRequired(t => t.Person).WithMany().HasForeignKey(d => d.PersonID);
}
#endregion
}
internal partial class PersonExpirableCredentialMapping : EntityTypeConfiguration<personexpirablecredential>
{
#region Constructors
public PersonExpirableCredentialMapping()
{
this.Map(m =>
{
m.Requires("CredentialCategoryCode").HasValue("Expirable");
});
this.ToTable("PersonCredential");
}
#endregion
}
以下是数据库模型:
现在在我的数据访问层中,当我检索此人并尝试访问“Person.PersonExpirableCredentials”时。它使用无效列名“Person_PersonID”抛出错误。下面是SQL查询,它生成。
exec sp_executesql N'SELECT
[Extent1].[PersonID] AS [PersonID],
''1X0X'' AS [C1],
[Extent1].[PersonCredentialID] AS [PersonCredentialID],
[Extent1].[WhenEffective] AS [WhenEffective],
[Extent1].[WhenExpire] AS [WhenExpire],
[Extent1].[WhenCompleted] AS [WhenCompleted],
[Extent1].[CredentialNumber] AS [CredentialNumber],
[Extent1].[Person_PersonID] AS [Person_PersonID]
FROM [dbo].[PersonCredential] AS [Extent1]
WHERE ([Extent1].[Person_PersonID] IS NOT NULL) AND ([Extent1].[Person_PersonID] = @EntityKeyValue1) AND ([Extent1].[CredentialCategoryCode] = ''Expirable'')',N'@EntityKeyValue1 int',@EntityKeyValue1=3
由于某种原因,EF无法识别Person类和子类PersonExpirableCredentials之间的关系。
请帮助。
由于
答案 0 :(得分:0)
试试这个
this.HasRequired(t => t.Person).WithMany(p => p.PersonExpirableCredentials).HasForeignKey(d => d.PersonID);
将您的收藏修改为基本类型
public virtual ICollection<PersonCredential> PersonExpirableCredentials
{
get;
set;
}
答案 1 :(得分:0)
您必须将PersonID
的getter和setter都设置为public。
public int PersonID
{
get;
set;
}
有几个地方你设置了setter受保护。将这些更改为公开。