我收到以下错误。
消息:Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时发生错误。有关详细信息,请参见内部异常。 ----> System.Data.SqlClient.SqlException:无效的列名'EmploymentTypeEntityEmploymentTypeID'。
将我的实体类名称和实体属性名称结合起来很奇怪。
下面是我的代码。
SystemTest.cs
library(thriftr)
pingpong_thrift = thriftpy::t_load("pingpong.thrift",
module_name="pingpong_thrift")
client = thriftpy::make_client(pingpong_thrift$PingPong, "127.0.0.1", 6000)
cut(client$ping())
EmploymentTypeGroup.cs
using (var transaction = _referenceDataDbContext.Database.BeginTransaction())
{
_referenceDataDbContext.EmploymentType.AddRangeAsync(
new EmploymentTypeEntity
{
EmploymentTypeID = 1,
EmploymentType = "EmploymentType",
CategoryTypeID = 27,
SiteAddress = null,
CreatedBy = "UnitTest",
CreatedOn = DateTime.Now,
ModifiedBy = "UnitTest",
ModifiedOn = DateTime.Now,
RowVersion = new RowVersion(1),
EmploymentTypeGroups = new[]
{
new EmploymentTypeGroupEntity
{
EmploymentTypeGroupID = 11, GroupName = "Child Care", IsActive = true
}
}
}
}
);
_referenceDataDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [ref].[EmploymentType] ON");
_referenceDataDbContext.SaveChanges();
}
EmploymentType.cs
public class EmploymentTypeGroupEntity
{
[Key]
public int? EmploymentTypeGroupID { get; set; }
public string GroupName { get; set; }
public bool? IsActive { get; set; }
public DateTime? CreatedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
public string ModifiedBy { get; set; }
[Timestamp]
[ConcurrencyCheck]
public byte[] RowVersion { get; set; }
}
DataDbContext.cs
public class EmploymentTypeEntity
{
[Key]
public int? EmploymentTypeID { get; set; }
public string EmploymentType { get; set; }
public int? CategoryTypeID { get; set; }
public bool? SiteAddress { get; set; }
public IEnumerable<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }
public DateTime? CreatedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
public string ModifiedBy { get; set; }
[Timestamp]
[ConcurrencyCheck]
public byte[] RowVersion { get; set; }
}
答案 0 :(得分:1)
您正在创建EmploymentTypeGroupEntity和EmploymentTypeEntity之间的关系。但是您并没有告诉Entity Framework那是什么关系。 EF猜想您想要在EmploymentTypeGroupEntity表中引用EmploymentTypeEntity,并为此创建了一个字段。显然这不是您想要的。
您需要告诉EF关系是什么。如果是一对多关系,其中一个employmentTypeEntity可以具有许多employmentTypeGroupEntity,则这种情况似乎是因为您已定义:
<Style TargetType="ComboBoxItem">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Grid x:Name="LayoutRoot" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
// What should go here?
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您还需要在EmploymentTypeGroupEntity类中创建一个外键。因此添加到此类:
public IEnumerable<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }
在EmploymentTypeEntity类中,更改集合类型:
public int EmploymentTypeEntityID { get; set; }
[ForeignKey(EmploymentTypeEntityID)]
public EmploymentTypeEntity EmploymentTypeEntity { get; set; }
添加一个构造函数,将新的List()分配给EmploymentTypeGroups。
在测试中更改数组分配以添加到集合中,并将外键添加到组创建中。