Copy Entity Framework Object

前端 未结 5 1807
面向向阳花
面向向阳花 2021-01-31 21:52

I have a EF4.1 class X and I want to make copy of that plus all its child records. X.Y and X.Y.Z

Now if I do the following it returns error.

The property \'X.ID\

5条回答
  •  春和景丽
    2021-01-31 22:27

    I use Newtonsoft.Json, and this awesome function.

        private static T CloneJson(T source)
        {
            return ReferenceEquals(source, null) ? default(T) : JsonConvert.DeserializeObject(JsonConvert.SerializeObject(source));
        }
    

提交回复
热议问题