EF DbSet.Find() returns 分离实体
EF DbSet.Find() returns Detached Entity
我有一个非常简单的 Entity
,只有 4 个字段:名称、州、城市和 ID 以及一些验证。
[Table("Places")]
public class Place : BindableBase, IDataErrorInfo
{
private string name;
private string city;
private Estado state;
private int id;
[Required]
[StringLength(500)]
public string Name
{
get { return this.name; }
set { SetProperty(ref name, value); }
}
[Required]
[StringLength(500)]
public string City
{
get { return this.city; }
set { SetProperty(ref city, value); }
}
[Required]
public State State
{
get { return this.state; }
set { SetProperty(ref state, value); }
}
[Key]
public int Id
{
get { return this.id; }
set { SetProperty(ref id, value); }
}
[NotMapped]
public bool IsValid
{
get
{
return Validator.TryValidateObject(this, new ValidationContext(this), new Collection<ValidationResult>(), true);
}
}
#region IDataErrorInfo Members
/// <summary>
/// IDataErrorInfo Interface Error Message for the object.
/// </summary>
[NotMapped]
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string propertyName]
{
get
{
var context = new ValidationContext(this)
{
MemberName = propertyName
};
var results = new Collection<ValidationResult>();
bool isValid = Validator.TryValidateObject(this, context, results, true);
if (!isValid)
{
ValidationResult result = results.SingleOrDefault(p =>
p.MemberNames.Any(memberName =>
memberName == propertyName));
return result == null ? null : result.ErrorMessage;
}
return null;
}
}
#endregion
}
我可以很好地将它添加到数据库中,但是一旦我尝试更新它,就会出现异常。
我正在使用:
public void UpdatePlace(Place place)
{
var entity = context.Places.Find(place.Id);
if (entity == null)
{
throw new InvalidOperationException("Place not found.");
}
context.Entry(place).CurrentValues.SetValues(place);
context.SaveChanges();
}
当我到达
context.Entry(place).CurrentValues.SetValues(place);
我得到一个异常:
System.InvalidOperationException
"Member 'CurrentValues' cannot be called for the entity of type
'Place' because the entity does not exist in the context.
To add an entity to the context call the Add or Attach method of
DbSet."
context.Entry
告诉我实体确实是分离的
但是 DbSet.Find()
方法的文档清楚地表明 Find()
应该 return 一个附加的实体,以防在数据库中找到:
Finds an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.
所以当我尝试获取 CurrentValues
时,由于实体已分离,它会抛出 Exception
... 但据我所知,应该有一个附加实体,或者null,没有别的....
我在网上找不到关于此错误的任何信息,我正在使用 SQL CE 4.0 作为数据库,有人知道发生了什么吗?
我想我每次从 Find
获取实体时都可以 Attach
,但我仍然想了解我的软件发生了什么,因为这不应该发生。
我认为您应该将此行更改为:
context.Entry(entity).CurrentValues.SetValues(place);
我有一个非常简单的 Entity
,只有 4 个字段:名称、州、城市和 ID 以及一些验证。
[Table("Places")]
public class Place : BindableBase, IDataErrorInfo
{
private string name;
private string city;
private Estado state;
private int id;
[Required]
[StringLength(500)]
public string Name
{
get { return this.name; }
set { SetProperty(ref name, value); }
}
[Required]
[StringLength(500)]
public string City
{
get { return this.city; }
set { SetProperty(ref city, value); }
}
[Required]
public State State
{
get { return this.state; }
set { SetProperty(ref state, value); }
}
[Key]
public int Id
{
get { return this.id; }
set { SetProperty(ref id, value); }
}
[NotMapped]
public bool IsValid
{
get
{
return Validator.TryValidateObject(this, new ValidationContext(this), new Collection<ValidationResult>(), true);
}
}
#region IDataErrorInfo Members
/// <summary>
/// IDataErrorInfo Interface Error Message for the object.
/// </summary>
[NotMapped]
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string propertyName]
{
get
{
var context = new ValidationContext(this)
{
MemberName = propertyName
};
var results = new Collection<ValidationResult>();
bool isValid = Validator.TryValidateObject(this, context, results, true);
if (!isValid)
{
ValidationResult result = results.SingleOrDefault(p =>
p.MemberNames.Any(memberName =>
memberName == propertyName));
return result == null ? null : result.ErrorMessage;
}
return null;
}
}
#endregion
}
我可以很好地将它添加到数据库中,但是一旦我尝试更新它,就会出现异常。
我正在使用:
public void UpdatePlace(Place place)
{
var entity = context.Places.Find(place.Id);
if (entity == null)
{
throw new InvalidOperationException("Place not found.");
}
context.Entry(place).CurrentValues.SetValues(place);
context.SaveChanges();
}
当我到达
context.Entry(place).CurrentValues.SetValues(place);
我得到一个异常:
System.InvalidOperationException
"Member 'CurrentValues' cannot be called for the entity of type 'Place' because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet."
context.Entry
告诉我实体确实是分离的
但是 DbSet.Find()
方法的文档清楚地表明 Find()
应该 return 一个附加的实体,以防在数据库中找到:
Finds an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.
所以当我尝试获取 CurrentValues
时,由于实体已分离,它会抛出 Exception
... 但据我所知,应该有一个附加实体,或者null,没有别的....
我在网上找不到关于此错误的任何信息,我正在使用 SQL CE 4.0 作为数据库,有人知道发生了什么吗?
我想我每次从 Find
获取实体时都可以 Attach
,但我仍然想了解我的软件发生了什么,因为这不应该发生。
我认为您应该将此行更改为:
context.Entry(entity).CurrentValues.SetValues(place);