合并数据上下文之外的对象

发布于 2024-09-11 22:42:28 字数 1772 浏览 3 评论 0原文

我有以下问题: 使用表单很容易在数据库中插入对象。

  1. 只需创建一个对象
  2. ,将其链接到您的 from 中的字段。
  3. 发回控制器,
  4. 创建一个新的 datacontext 并执行 datacontext.InsertOnSubmit(object)

public static void AddPage(string lang, Page page)
                {
                    using (var db = new CardReaderDataContext())
                    {
                        page.Lang = lang;
                        page.URL = UrlHelper.CreateValidSeoUrl(page.Name, "-");
                        db.Pages.InsertOnSubmit(page);
                        db.SubmitChanges();
                    }
                }

但如果你想更新一个对象,那就是一项繁琐的工作。 您执行相同的流程,

  1. 获得对象,
  2. 将其链接到您的表单,
  3. 发布它,但是然后!!! 因为它超出了您的数据上下文,所以您必须从数据上下文重新加载对象,
  4. 传输所有变量并保存它, 这解释起来有点复杂,所以我举一个例子:

更新您在表单中修改的对象:

public static void Update(Page page)
        {
            using (var db = new CardReaderDataContext())
            {
                var _page = db.Pages.Where(p => p.Guid == page.Guid).Single();
                _page.ModificationDate = DateTime.Now;
                _page.Title = page.Title;
                _page.Description = page.Description;
                _page.Content = page.Content;
                _page.Keywords = page.Keywords;
                _page.Name = page.Name;
                _page.WTLang = page.WTLang;
                _page.WTSKU = page.WTSKU;
                _page.WTTi = page.WTTi;
                _page.WTUri = page.WTUri;
                _page.URL = UrlHelper.CreateValidSeoUrl(page.Name, "-");
                //  _page.Order = GetMaxOrderByMenuGuid(page.MenuGuid);
                db.SubmitChanges();
            }
        }

我不知道是否清楚,如果不评论我,我会编辑

I have the following question:
It is easy to insert an oBject in database with a form.

  1. Just create an object
  2. link it to the fields in your from.
  3. Post back to controller,
  4. create a new datacontext and do datacontext.InsertOnSubmit(object)

.

public static void AddPage(string lang, Page page)
                {
                    using (var db = new CardReaderDataContext())
                    {
                        page.Lang = lang;
                        page.URL = UrlHelper.CreateValidSeoUrl(page.Name, "-");
                        db.Pages.InsertOnSubmit(page);
                        db.SubmitChanges();
                    }
                }

But if you want to update an object, it is a tedious job.
You do the same flow,

  1. you get the object,
  2. link it to your form,
  3. post it, but THEN !!!
    because it went outside your datacontext, you have to reload the object from the datacontext,
  4. transfer all the variables and save it,
    this is a little complex explained so I give an example:

To update an object that you modified in a form:

public static void Update(Page page)
        {
            using (var db = new CardReaderDataContext())
            {
                var _page = db.Pages.Where(p => p.Guid == page.Guid).Single();
                _page.ModificationDate = DateTime.Now;
                _page.Title = page.Title;
                _page.Description = page.Description;
                _page.Content = page.Content;
                _page.Keywords = page.Keywords;
                _page.Name = page.Name;
                _page.WTLang = page.WTLang;
                _page.WTSKU = page.WTSKU;
                _page.WTTi = page.WTTi;
                _page.WTUri = page.WTUri;
                _page.URL = UrlHelper.CreateValidSeoUrl(page.Name, "-");
                //  _page.Order = GetMaxOrderByMenuGuid(page.MenuGuid);
                db.SubmitChanges();
            }
        }

I don't know if it is clear, if it isn't comment me, I will edit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2024-09-18 22:42:28

我认为您正在寻找 DataContext.Attach,但您只能将其与已序列化/反序列化的 linqtosql 对象一起使用。

阅读这个问题的答案 -
http://social .msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/384a1c03-3acf-43ef-9a25-b84f93025e63/

“即使
尝试获取旧版本。经过
这样做你实际上是在转向
关闭乐观并发,所以除非
你故意认为这是一件坏事
方法。你需要做的是
往返原始状态和
对象的当前状态。”

I think you're looking for DataContext.Attach, but you can only use that with linqtosql objects that have been serialised/deserialised.

Have a read of the answer to this question -
http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/384a1c03-3acf-43ef-9a25-b84f93025e63/

"It's also not a good idea to even
attempt to fetch the old version. By
doing that you are in effect turning
off optimistic concurrency, so unless
you intended that this is a bad
approach. What you need to do is
round trip both the original state and
the current state of the object."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文