gpt4 book ai didi

entity-framework-4 - Entity Framework 4 - 与 CTP5 View 的一对多关系(代码优先)

转载 作者:行者123 更新时间:2023-12-04 06:32:30 27 4
gpt4 key购买 nike

我试图在两个实体之间映射 1-M 关系,其中第一个实体通常映射到表,第二个实体从 View 中获取。

涉及的实体是:

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

问题是该模型未经验证:

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)



如果我删除“.HasForeignKey(t => t.InstituteId);”生成的查询包含对名为 InstituteId1 的字段的引用,该字段在查询 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

有什么建议吗?谢谢。

最佳答案

显然,您的 View 似乎没有所需的 key 。
尝试更改

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



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

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

关于entity-framework-4 - Entity Framework 4 - 与 CTP5 View 的一对多关系(代码优先),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5233810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com