EF-Code First 对象不反映后台数据库更改
EF-Code First Objects don't reflect background DB changes
我有 3 个 类,它们首先通过 entity framework 代码映射到数据库表。
class Device {
public int Id {get;set;}
public virtual List<Point> Points {get;set;}
}
class Point {
public int Id {get;set;}
public string Name {get;set;}
public virtual List<Record> Records {get;set;}
public virtual Device Device {get;set;}
}
class Record {
public int Id {get;set;}
public DateTime Date {get;set;}
public double Value {get;set;}
public virtual Point Point {get;set;}
}
每个 Device
都有一个 Points
的列表,每个 Point
都有一个 Records
的列表,具有返回父级的相应导航属性。这一切都很好,除了与数据库的连接在序列化时丢失。
问题:我有一个 Device
满 Points
,每个包含 10 个 Records
。我序列化 Device
并关闭我的应用程序。同时,收集服务应用程序向数据库中的每个 Point
添加了 50 个 Records
。当我启动我的原始应用程序并反序列化 Device
时,这些点仅包含我的第一个 运行 中的 10 Records
而不是实际存在于 [=] 上的 60 Records
13=] 在数据库中。
我是不是想错了?有什么方法可以强制 point.Records
始终从数据库中获取 Records
吗?
TL;DR:是的,你想错了。
Entity Framework 是一种 ORM(对象关系映射):换句话说,它是将对象 link 到关系数据库并使这些关系持久保存到对象模型的一种方法。
然而,它不是双向绑定。你说你从数据库中拉取了对象,然后序列化了,然后反序列化的时候,你惊奇的发现和你序列化的时候一模一样。
The problem: I have a Device full of Points, each containing 10
Records. I serialize the Device and close my application. Meanwhile, a
collection service application adds 50 more Records to each Point in
the database. When I start my original app and deserialize the Device,
those points only contain the 10 Records from my first run and not the
60 Records that actually exist on that Point in the database.
想一想:您在某处保存了一组对象,当您检索它们时惊讶地发现它们是相同的。如果它以任何其他方式起作用,我会感到震惊!
让他们更新并显示在数据库中所做的更改的唯一方法是实际转到数据库 并检索更改。
我有 3 个 类,它们首先通过 entity framework 代码映射到数据库表。
class Device {
public int Id {get;set;}
public virtual List<Point> Points {get;set;}
}
class Point {
public int Id {get;set;}
public string Name {get;set;}
public virtual List<Record> Records {get;set;}
public virtual Device Device {get;set;}
}
class Record {
public int Id {get;set;}
public DateTime Date {get;set;}
public double Value {get;set;}
public virtual Point Point {get;set;}
}
每个 Device
都有一个 Points
的列表,每个 Point
都有一个 Records
的列表,具有返回父级的相应导航属性。这一切都很好,除了与数据库的连接在序列化时丢失。
问题:我有一个 Device
满 Points
,每个包含 10 个 Records
。我序列化 Device
并关闭我的应用程序。同时,收集服务应用程序向数据库中的每个 Point
添加了 50 个 Records
。当我启动我的原始应用程序并反序列化 Device
时,这些点仅包含我的第一个 运行 中的 10 Records
而不是实际存在于 [=] 上的 60 Records
13=] 在数据库中。
我是不是想错了?有什么方法可以强制 point.Records
始终从数据库中获取 Records
吗?
TL;DR:是的,你想错了。
Entity Framework 是一种 ORM(对象关系映射):换句话说,它是将对象 link 到关系数据库并使这些关系持久保存到对象模型的一种方法。
然而,它不是双向绑定。你说你从数据库中拉取了对象,然后序列化了,然后反序列化的时候,你惊奇的发现和你序列化的时候一模一样。
The problem: I have a Device full of Points, each containing 10 Records. I serialize the Device and close my application. Meanwhile, a collection service application adds 50 more Records to each Point in the database. When I start my original app and deserialize the Device, those points only contain the 10 Records from my first run and not the 60 Records that actually exist on that Point in the database.
想一想:您在某处保存了一组对象,当您检索它们时惊讶地发现它们是相同的。如果它以任何其他方式起作用,我会感到震惊!
让他们更新并显示在数据库中所做的更改的唯一方法是实际转到数据库 并检索更改。