C# Linq To SQL 附加/刷新实体对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/898267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq To SQL Attach/Refresh Entity Object
提问by Ronnie Overby
In Linq To Sql, when updating one of my entities, Faculty, I am creating a new instance of the Faculty object, then initializing someof the properties with values supplied by the user.
在 Linq To Sql 中,当更新我的实体之一 Faculty 时,我正在创建 Faculty 对象的一个新实例,然后使用用户提供的值初始化一些属性。
If I attach this new object to the entity set, and submit changes, the properties that I didn't set take on the default value of whatever datatype they are.
如果我将这个新对象附加到实体集并提交更改,那么我没有设置的属性将采用它们的任何数据类型的默认值。
How can I refresh the new object so that the properties that have been set keep their values and the properties that haven't been set get the values from the database?
如何刷新新对象,以便已设置的属性保留其值而未设置的属性从数据库中获取值?
Thanks
谢谢
采纳答案by a_hardin
Did you try
你试过了吗
context.Refresh(RefreshMode.OverwriteCurrentValues, faculty);
context.Refresh(RefreshMode.OverwriteCurrentValues, faculty);
after submit changes where context
is your linq2sql datacontext and faculty
is the entity you want to refresh?
提交更改后,context
您的 linq2sql 数据上下文在哪里,faculty
您要刷新的实体是什么?
回答by CSharpAtl
What about retrieving the object from the database, then changing the appropriate values, then submitting the update?
从数据库中检索对象,然后更改适当的值,然后提交更新怎么样?