s***o 发帖数: 2191 | 1 http://weblogs.asp.net/scottgu/archive/2012/07/19/entity-framew
So there are two different versions of Microsoft - DevDiv is more and more
open while WinDiv is more and more closed |
o****e 发帖数: 916 | 2 正好问个EF的问题
user table有个ID field
product table 有个 UserID field, 是user id的foreign key
现在有个database的entities object,比如 _entities
User user = _entities.Users.First(u=>u.ID == 1)
list user.Products
这时有另外一个连接对数据库操作,删除了user 1的一个product
但如果对原先的 _entities 再运行代码
user = _entities.Users.First(u=>u.ID == 1)
返回的user.Products collection还是原来的,删掉那个还在,试了refresh user
object也没有用。
求高手指点!谢谢! |
k****i 发帖数: 1072 | 3 _entities.Entry(user).Collection(p => p.Products).CurrentValue.Clear();
_entities.Entry(user).Collection(p => p.Products).Load();
【在 o****e 的大作中提到】 : 正好问个EF的问题 : user table有个ID field : product table 有个 UserID field, 是user id的foreign key : 现在有个database的entities object,比如 _entities : User user = _entities.Users.First(u=>u.ID == 1) : list user.Products : 这时有另外一个连接对数据库操作,删除了user 1的一个product : 但如果对原先的 _entities 再运行代码 : user = _entities.Users.First(u=>u.ID == 1) : 返回的user.Products collection还是原来的,删掉那个还在,试了refresh user
|
c**d 发帖数: 579 | 4 这个写法很容易出这种奇怪的错。如果需要用到child objects的话,直接eager
loading,
_entities.Include(u => u.Products).First(u => u.ID == 1) |
o****e 发帖数: 916 | 5 多谢回复,Entry extension 在EF 4.1才支持,我还在用4.0,没能试一下
【在 k****i 的大作中提到】 : _entities.Entry(user).Collection(p => p.Products).CurrentValue.Clear(); : _entities.Entry(user).Collection(p => p.Products).Load();
|
o****e 发帖数: 916 | 6 多谢回复,刚试了一下,一样的结果,删掉的product还在...
【在 c**d 的大作中提到】 : 这个写法很容易出这种奇怪的错。如果需要用到child objects的话,直接eager : loading, : _entities.Include(u => u.Products).First(u => u.ID == 1)
|
S***k 发帖数: 370 | 7 the instance _entities holds the initial info loaded from the database.
a new instance of the database entity is needed to get the latest records in
database.
【在 o****e 的大作中提到】 : 正好问个EF的问题 : user table有个ID field : product table 有个 UserID field, 是user id的foreign key : 现在有个database的entities object,比如 _entities : User user = _entities.Users.First(u=>u.ID == 1) : list user.Products : 这时有另外一个连接对数据库操作,删除了user 1的一个product : 但如果对原先的 _entities 再运行代码 : user = _entities.Users.First(u=>u.ID == 1) : 返回的user.Products collection还是原来的,删掉那个还在,试了refresh user
|
s***o 发帖数: 2191 | 8 What about calling "Refresh" at "Product" level?
context.Refresh(RefreshMode.StoreWins, yourProductCollection);
btw, it sounds like you are using a long-lasting global context instance?
【在 o****e 的大作中提到】 : 正好问个EF的问题 : user table有个ID field : product table 有个 UserID field, 是user id的foreign key : 现在有个database的entities object,比如 _entities : User user = _entities.Users.First(u=>u.ID == 1) : list user.Products : 这时有另外一个连接对数据库操作,删除了user 1的一个product : 但如果对原先的 _entities 再运行代码 : user = _entities.Users.First(u=>u.ID == 1) : 返回的user.Products collection还是原来的,删掉那个还在,试了refresh user
|
o****e 发帖数: 916 | 9 yes, I used a singleton to access database, context is a static in that
class. I took this approach to avoid overhead of creating db connection
every time a query is made, as the web server and database are in different
machines. I will try out the refresh on collection.
【在 s***o 的大作中提到】 : What about calling "Refresh" at "Product" level? : context.Refresh(RefreshMode.StoreWins, yourProductCollection); : btw, it sounds like you are using a long-lasting global context instance?
|
S***k 发帖数: 370 | 10 This could help:
http://stackoverflow.com/questions/3653009/entity-framework-and
Creating a context does not cost too much. If your application is web based
and accessed by multiple users, creating a new context for each request and
disposing the context instance by "using" could help to reduce the DB load
caused by the refreshing.
different
【在 o****e 的大作中提到】 : yes, I used a singleton to access database, context is a static in that : class. I took this approach to avoid overhead of creating db connection : every time a query is made, as the web server and database are in different : machines. I will try out the refresh on collection.
|