[英]The property on entity type cannot be used for objects of type Nullable1 with Entity Framework
The line dataContext.Entry(this).Property(property).IsModified = true;
行dataContext.Entry(this).Property(property).IsModified = true;
is causing this error below.导致以下错误。 This doesn't happen with non foreign key properties.非外键属性不会发生这种情况。 Any clue why?任何线索为什么?
The property 'RankingId' on entity type 'TeamOrganizationSeason' cannot be used for objects of type Nullable1 because it is a property for objects of type Nullable1.实体类型“TeamOrganizationSeason”上的属性“RankingId”不能用于 Nullable1 类型的对象,因为它是 Nullable1 类型对象的属性。
public class TeamOrganizationSeason
{
public int? RankingId { get; set; }
[ForeignKey("RankingId")]
public Ranking Ranking { get; set; }
public void IsUpdated(TeamOrganizationSeason teamOrganizationSeason, DataContext dataContext)
{
Update(RankingId, teamOrganizationSeason.RankingId, dataContext, t => t.RankingId);
}
private void Update(double? current, double? original, DataContext dataContext, Expression<Func<TeamOrganizationSeason, double?>> property)
{
if (current != original)
{
var state = dataContext.Entry(this).State;
dataContext.TeamOrganizationSeasons.Attach(this);
dataContext.Entry(this).Property(property).IsModified = true;
};
}
}
This had to do not with nullable issues but with different types.这与可空问题无关,而是与不同类型有关。 An int
being sent instead of a double
was causing this error.发送int
而不是double
导致此错误。 So we made it generic and all is well.所以我们把它变成通用的,一切都很好。
Causing Code Error导致代码错误
if (!requestedType.IsAssignableFrom(propertyType))
{
throw Error.DbEntityEntry_WrongGenericForProp(
propertyName, declaringType.Name, requestedType.Name, propertyType.Name);
}
Generic通用的
private void Update<T>(double? current, double? original, DataContext dataContext, Expression<Func<TeamOrganizationSeason, T>> property)
{
if (current != original)
{
if (dataContext.Entry(this).State == EntityState.Detached)
{
dataContext.TeamOrganizationSeasons.Attach(this);
}
dataContext.Entry(this).Property(property).IsModified = true;
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.