- Published on
EntityFramework - DisconnectedEntity
EntityFramework disconnect 2 connect
Course c = new Course();
using (var db = new ContosoUniversityEntities())
{
c = db.Course.Find(2);
Console.WriteLine(db.Entry(c).State); // Unchange
c.Title += ".";
Console.WriteLine(db.Entry(c).State); // Midified
}
using (var db = new ContosoUniversityEntities())
{
Console.WriteLine(db.Entry(c).State); // Detached
db.Course.Attach(c);
Console.WriteLine(db.Entry(c).State); // Unchange
db.Entry(c).State = EntityState.Modified;
Console.WriteLine(db.Entry(c).State); // Midified
db.SaveChanges();
}
修改狀態時導覽屬性的連動
- EntityState.Added 所有導覽屬性也會一併新增
- EntityState.Modified 所有導覽屬性不會一併更新
- EntityState.Deleted 所有導覽屬性不會一併刪除
- EntityState.Unchanged