Entity framework 4 实体框架4-与CTP5视图的一对多关系(代码优先)
我试图映射两个实体之间的1-m关系,其中第一个实体通常映射到表,第二个实体从视图中获取 涉及的实体包括:Entity framework 4 实体框架4-与CTP5视图的一对多关系(代码优先),entity-framework-4,poco,code-first,entity-framework-ctp5,ef-code-first,Entity Framework 4,Poco,Code First,Entity Framework Ctp5,Ef Code First,我试图映射两个实体之间的1-m关系,其中第一个实体通常映射到表,第二个实体从视图中获取 涉及的实体包括: public class Institute { public int Id { get; set; } public string Name { get; set; } //... public virtual ICollection<Text> Texts { get; set; } } public class Text { pub
public class Institute
{
public int Id { get; set; }
public string Name { get; set; }
//...
public virtual ICollection<Text> Texts { get; set; }
}
public class Text
{
public int InstituteId { get; set; }
public int TextId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public bool IsRequired { get; set; }
public int? MaxLength { get; set; }
}
有什么建议吗?谢谢。显然,您的视图似乎没有所需的键。 试着改变 到
这将有助于识别关系以及密钥TextId。ok!它工作正常,但我真的不明白它为什么工作。你能解释清楚吗?谢谢问题是您试图在文本模型上设置forieng键InstituteId,同时您说InstituteId是文本上唯一的键。CF无法识别,因为您需要1:*关系(因为您在Institute中有一个集合),但无论您两侧的键是否相同(这可能意味着1:1关系)。希望这次我是清楚的,而不是迷惑:)
private void MapInstituteText(EntityTypeConfiguration<InstituteText> text)
{
//From a view
text.HasKey(i => i.InstituteId)
.ToTable("vwInstituteTexts");
text.Property(i => i.InstituteId)
.HasColumnName("FKInstituteID")
.IsRequired();
text.Property(i => i.TextPropertyId)
.HasColumnName("FKTextPropertyID")
.IsRequired();
text.Property(i => i.Name)
.HasColumnName("Name")
.IsRequired();
text.Property(i => i.Value)
.HasColumnName("Value");
text.Property(i => i.IsRequired)
.IsRequired();
text.Property(i => i.MaxLength);
}
private void MapInstitute(EntityTypeConfiguration<Institute> institute)
{
institute.HasKey(i => i.Id)
.ToTable("Institutes");
institute.Property(i => i.Id)
.HasColumnName("ID")
.IsRequired();
institute.Property(i => i.Name)
.HasColumnName("Name")
.HasMaxLength(128)
.IsRequired();
institute
.HasMany(i => i.Texts)
.WithRequired()
.HasForeignKey(t => t.InstituteId);
}
exec sp_executesql N'SELECT
[Extent1].[FKInstituteID] AS [FKInstituteID],
[Extent1].[FKTextPropertyID] AS [FKTextPropertyID],
[Extent1].[Name] AS [Name],
[Extent1].[Value] AS [Value],
[Extent1].[IsRequired] AS [IsRequired],
[Extent1].[MaxLength] AS [MaxLength],
[Extent1].[InstituteId1] AS [InstituteId1]
FROM [institute].[vwInstituteTexts] AS [Extent1]
WHERE [Extent1].[InstituteId1] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1360
text.HasKey(i => i.InstituteId)
.ToTable("vwInstituteTexts");
text.HasKey(i => new {i.InstituteId, i.TextId}).ToTable("vwInstituteTexts")