每个应用只有一个 DataContext 并通过单例共享它可以吗?
我问这个是因为我想在每一种形式中都有 DataContext,但我意识到,如果我改变了一个 DataContext 中的一些实体,我必须刷新它,如果以前使用过的话。
例如 form1:
db = GetContext()
item=(from p in db.Table where p.id=1 select p)
在另一种形式上
db = GetContext()
item=(from p in db.Table where p.id=1 select p)
item.value="test"
回到我要做的原始表格
db.Refresh(RefreshMode.OverwriteCurrentValues, item)
即使我做了一个新的
item=(from p in db.Table where p.id=1 select p)
(不刷新)值不会更新
DataContext 是线程安全的吗?
最佳答案
Would it be ok to have only one DataContext per app and share that through an singleton?
嗯,这肯定不是它设计的目的 - 如果您有多个线程执行多个操作,它肯定不是一个好主意。
就像数据库连接一样,最好在需要时创建上下文,做任何需要做的事情,然后在需要执行一组新操作时创建一个新上下文。
关于c# - Linq 和数据上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12871666/