在使用LINQ to SQL时,SubmitChanges()方法用于将更改提交到数据库。要判断SubmitChanges()是否实际更改了特定实体的任何内容,可以使用以下方法:
int changes = dataContext.SubmitChanges();
if (changes > 0)
{
Console.WriteLine("Changes submitted: {0}", changes);
}
else
{
Console.WriteLine("No changes submitted");
}
if (entity.State == EntityState.Modified)
{
Console.WriteLine("Entity has been modified");
}
else
{
Console.WriteLine("Entity has not been modified");
}
entity.PropertyChanging += (sender, e) =>
{
Console.WriteLine("Property {0} is changing", e.PropertyName);
};
entity.PropertyChanged += (sender, e) =>
{
Console.WriteLine("Property {0} has been changed", e.PropertyName);
};
通过以上方法,可以判断SubmitChanges()是否实际更改了特定实体的任何内容。
领取专属 10元无门槛券
手把手带您无忧上云