首先使用代码保存和还原断开连接的实体的正确方法是什么? [英] What is the right way to save and restore a disconnected entity using code first?

查看:39
本文介绍了首先使用代码保存和还原断开连接的实体的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了存储用户的屏幕首选项,我拥有要在程序启动时检索并在程序结束时保存的ScreenSettings实体。

So that I can store the user's screen preferences, I have ScreenSettings entity that I want to retrieve when the program starts and save when the program ends.

为此我不想保持上下文开放的原因。

For this reason I don't want to keep the context open.

我想知道这样做的最佳方法。
我尝试了以下
,但是我对SaveSettings函数不满意,因为它会删除并重新添加对象。

I am wondering about the best way to do this. I have tried the following however I am not comfortable with the SaveSettings function because it deletes and re-adds the object.

我该如何

namespace ClassLibrary1
{
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

//Domain Class
public class ScreenSetting
{
    #region Properties

    public int Id { get; set; }

    [Required]
    public int WindowLeft { get; set; }

    [Required]
    public int WindowTop { get; set; }

    #endregion
}

// Context
public class Context : DbContext
{
    #region Properties

    public DbSet<ScreenSetting> ScreenSettings { get; set; }

    #endregion
}

// UI
public class UI
{
    #region Public Methods

    // Get the settings object
    public ScreenSetting GetSettings(int SettingsId)
    {
        var Db = new Context();
        ScreenSetting settings = Db.ScreenSettings.Find(SettingsId);
        if (settings == null)
        {
            settings = new ScreenSetting { Id = SettingsId, WindowTop = 100, WindowLeft = 100 };
            Db.ScreenSettings.Add(settings);
        }
        Db.Dispose();
        return settings;
    }

    // Save the settings object
    public void SaveSettings(ScreenSetting settings)
    {
        var Db = new Context();
        ScreenSetting oldSettings = Db.ScreenSettings.Find(settings.Id);
        if (oldSettings == null)
        {
            Db.ScreenSettings.Add(settings);
        }
        else
        {
            Db.ScreenSettings.Remove(oldSettings);
            Db.ScreenSettings.Add(settings);
        }
        Db.Dispose();
    }

    public void test()
    {
        ScreenSetting setting = this.GetSettings(1);
        setting.WindowLeft = 500;
        setting.WindowTop = 500;
        this.SaveSettings(setting);
    }

    #endregion

    #region Methods

    private static void Main()

    {
        var o = new UI();
        o.test();
    }

    #endregion
}
}


推荐答案

您遇到了常见的模式,更新或插入,这种模式是如此普遍以至于它有一个名称: upsert 。当一种模式很常见时,通常也有一个通用的解决方案。

You ran into a common pattern, update or insert, which is so common that it's got a name: upsert. When a pattern is common, usually there also is a common solution.

System.Data.Entity.Migrations 没有扩展方法 AddOrUpdate 完全满足您的要求:

In System.Data.Entity.Migrations there is an extension method AddOrUpdate that does exactly what you want:

public void SaveSettings(ScreenSetting settings)
{
    using (var db = new Context())
    {
        db.ScreenSettings.AddOrUpdate(settings);
        db.SaveChanges();
    }
}

这篇关于首先使用代码保存和还原断开连接的实体的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆