如何使用Singleton Repository将Result或SubmitChanges附加到DataContext [英] How to Attach or SubmitChanges to DataContext using Singleton Repository

查看:55
本文介绍了如何使用Singleton Repository将Result或SubmitChanges附加到DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个Singleton Repository类,其中包含打开和关闭每个调用的datacontext所需的方法。我将主要应用程序中的这些方法称为:

        

`MyTemplate tmp1 =  Repository.Instance.Function1(10);`

I am implementing a Singleton Repository class with methods that are required to open and dismiss the datacontext for every call. I call these methods from the main application as:
        
`MyTemplate tmp1 =  Repository.Instance.Function1 (10);`

最后,我想要一个Save方法来保存一个新的"MyTemplate"。如何使用我的设计所基于的格式?

Somewhere at the end, I want to have a Save method to save a new "MyTemplate". How is that done with the format my design is based on?

         tmp1.property1 ="更改为其他内容";

         tmp1.property10 ="已更改"; // ....以及此处附带的许多复杂属性

         tmp1.property1 = "change to something else";
         tmp1.property10 = "changed"; // .... and many complicated properties attached here

          Repository.Instance.Save(TMP1);  //需要帮助实现!

          Repository.Instance.Save(tmp1);  // Need help implementing!

我也试过"Detach()"在线示例以及"克隆",但似乎没有什么对我有用。

I also have tried "Detach()" examples online as well as "Cloning", but nothing seem to work for me.

请注意我应该保留此设计,因此全局Datacontext等选项无效。表格"MyTemplate"有一个非常复杂的长


属性列表,所以它不像使用2个属性或复制/克隆模板那么容易。

Please be advised I am supposed to keep this design, so options like global Datacontext is not valid. The table "MyTemplate" has a very complicated long
list of properties so it's not as easy as working with 2 properties or copying/cloning the template.

     public sealed class Repository:IRepository,IDisposable

    {

    private static readonly Repository _instance = new Repository();

    public sealed class Repository: IRepository, IDisposable
    {
    private static readonly Repository _instance = new Repository();

    public static Repository Instance {get {return _instance; }
   

    private Repository(){}  //构造函数

    static Repository(){}

    public static Repository Instance { get { return _instance; } }
   
    private Repository() { }  // Constructors
    static Repository() { }

    // IRepository会员示例:

    public MyTemplate Function1(int i)

    {  //打开并关闭¥b $ b      DataContext db = new DataContext(@" ....");

      return db.mytemplates.where(x => x.Id == i).FirstOrDefault();

    }
    public MyTemplate Function2()

    {  //打开并关闭¥b $ b      DataContext db = new DataContext(@" ....");

      return db.someothercommand .....

    }

    // IRepository member examples:
    public MyTemplate Function1(int i)
    {  // Open and dismiss
      DataContext db = new DataContext(@"....");
      return db.mytemplates.where(x => x.Id == i).FirstOrDefault();
    }
    public MyTemplate Function2()
    {  // Open and dismiss
      DataContext db = new DataContext(@"....");
      return db.someothercommand.....
    }

    //需要在这里提供帮助:
$
    public Save(MyTemplate templateToSave){

    // NEED HELP HERE:
    public Save (MyTemplate templateToSave) {

      //我尝试了以下内容:
$
      //   克隆不起作用

      //   创建Partial类并实现Detach()不起作用。

      // I have tried the following:
      //    Cloning didn't work
      //    Created Partial class and implemented Detach() didn't work.

      // 试过:

      //  db.MyTemplates.Attach(templateToSave);

      //  db.SubmitChanges();

    } b $ b}

      //  Tried:
      //  db.MyTemplates.Attach(templateToSave);
      //  db.SubmitChanges();
    }
}

真的很感激任何帮助。我现在一直在尝试不同的方法。

Any help would really be appreciated. I have been trying different approaches for days now.

谢谢。

推荐答案

嗨codoiso ; $


因为每次调用函数时都要创建一个新的DbContext,所以在离开函数之前需要分离记录。然后,当您传递要保存的实体时,您需要附加记录/ s并将其状态更改为已修改。 Attach方法
调用将实体添加到UnChanged状态的上下文中,因此如果在将State更改为Modified之前执行SaveChanges,则不会发生更新。 

Hi codoiso;

Because you are creating a new DbContext each time you call a function you need to Detach the record/s before leaving the function. Then when you pass the entity to be saved you need to Attach the record/s and change it state to modified. The Attach method call adds the entity to the context in the UnChanged state and therefore if you do a SaveChanges before changing the State to Modified no update will occur. 

// IRepository member examples:
public MyTemplate Function1(int i)
{  // Open and dismiss
  DataContext db = new DataContext(@"....");
  var myTemplate = db.mytemplates.where(x => x.Id == i).FirstOrDefault();
  // This wwill detache the entity from the context.
  db.Entry(myTemplate).State = EntityState.Detached;
  return myTemplate;
}

// A generic method to handle attaching and saving changes to the db. 
public void Save<T>(T templateToSave) where T : class 
{
    DataContext db = new DataContext(@"....");

    db.Set<T>().Attach(templateToSave);
    db.Entry(templateToSave).State = EntityState.Modified;
    db.SaveChanges();
}

要调用Save函数,您需要传递要用作T参数的表,如下所示

To call the Save function you need to pass the table you wish to use as the T parameter, something like this

Save<MyTemplates>(templateToSave);

  

  


这篇关于如何使用Singleton Repository将Result或SubmitChanges附加到DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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