实体框架 4 - 与 CTP5 视图的一对多关系(代码优先)

发布于 2024-10-21 03:25:19 字数 2840 浏览 1 评论 0原文

我正在尝试映射两个实体之间的 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 抛出异常。 系统类型初始化异常: 系统类型初始化异常: 类型初始值设定项 '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

有任何建议吗?谢谢。

I'm attempting to map a 1-M relationship between two entities where the first one is normally mapped to a table and the second one is taken from a view.

The involved entities are:

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

The relevant mapping code is:

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

The problem is that the model is not validated:

Initialization method
Studentum.Core.Tests.InstituteTests.Initialize
threw exception.
System.TypeInitializationException:
System.TypeInitializationException:
The type initializer for
'Studentum.Core.FluentCoreRepositoryFactory'
threw an exception. --->
System.Data.Entity.ModelConfiguration.ModelValidationException:
One or more validation errors were
detected during model generation:

System.Data.Edm.EdmAssociationEnd: :
Multiplicity is not valid in Role
'Institute_InnerInstituteTexts_Target'
in relationship
'Institute_InnerInstituteTexts'.
Because the Dependent Role refers to
the key properties, the upper bound of
the multiplicity of the Dependent Role
must be 1.
(the names of the exception can not match exactly because I recreated some of the code specifically for this post)

If I remove the ".HasForeignKey(t => t.InstituteId);" the generated query contains a reference to a field called InstituteId1 which is not present in the queried view

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

Any suggestion? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-10-28 03:25:19

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

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


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

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

Apparently your View seems to be not having the required Keys.
Try changing

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

to


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

this will help recognize the relationship as well the key TextId.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文