Linq to sql add/update in different methods with different datacontexts

后端 未结 4 1562

I have to methods, Add() and Update() which both create a datacontext and returns the object created/updated.

In my unit test I call first Add(), do some stuff and

4条回答
  •  时光取名叫无心
    2021-01-07 07:39

    In order to update an entity that is attached to another data context, you will first need to detach it from the context and then attach it to the other context. One way to detach an object is as follows:

    object ICloneable.Clone()
        {
            var serializer = new DataContractSerializer(GetType());
            using (var ms = new System.IO.MemoryStream())
            {
                serializer.WriteObject(ms, this);
                ms.Position = 0;
                return serializer.ReadObject(ms);
            }
        }
    

提交回复
热议问题