【问题标题】:Entity Framework 4 - One-To-Many Relationship with a view with CTP5 (code first)实体框架 4 - 与 CTP5 视图的一对多关系(代码优先)
【发布时间】:2011-07-11 04:00:39
【问题描述】:

我正在尝试在两个实体之间映射 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
{
    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; }
}

相关映射代码为:

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

问题是模型没有经过验证:

初始化方法 Studentum.Core.Tests.InstituteTests.Initialize 抛出异常。 System.TypeInitializationException: System.TypeInitializationException: 类型初始化器 'Studentum.Core.FluentCoreRepositoryFactory' 抛出异常。 ---> System.Data.Entity.ModelConfiguration.ModelValidationException: 一个或多个验证错误是 在模型生成期间检测到:

System.Data.Edm.EdmAssociationEnd: : 多重性在角色中无效 'Institute_InnerInstituteTexts_Target' 谈恋爱 'Institute_InnerInstituteTexts'。 因为从属角色是指 关键属性,上限 从属角色的多样性 必须为 1。 (异常的名称不能完全匹配,因为我专门为这篇文章重新创建了一些代码)

如果我删除“.HasForeignKey(t => t.InstituteId);”生成的查询包含对名为 InstituteId1 的字段的引用,该字段在查询视图中不存在

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

有什么建议吗?谢谢。

【问题讨论】:

    标签: entity-framework-4 poco code-first entity-framework-ctp5 ef-code-first


    【解决方案1】:

    显然您的视图似乎没有所需的密钥。 尝试改变

    text.HasKey(i => i.InstituteId)
            .ToTable("vwInstituteTexts");
    

    
    text.HasKey(i => new {i.InstituteId, i.TextId}).ToTable("vwInstituteTexts")
    

    这将有助于识别关系以及关键 TextId。

    【讨论】:

    • 好的!它工作正常,但我真的不明白它为什么工作。你能更好地解释一下吗?谢谢!
    • 问题是您试图在 Text 模型上设置一个 forieng 键 InstituteId,同时您说 InstituteId 是 Text 上的唯一键。 CF 无法弄清楚,因为您想要一个 1:* 关系(因为您在 Institute 中有一个 collection),但是您在双方都有相同的键(这可能意味着 1:1 关系)。希望这次我清楚而不是混淆:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多