Answered by:
Cloning of Entitys

Question
-
Hi
I tried to find some answer into the related topics but most of them ar in C#.
What I am trying to to sounds very simple.
Bevore I save changes to an entity I will Save the old(original) Entity as a new object into the Database (for historysation).
In each entity I have three (technical) scalar values (validFrom, validTo and Version) If it is going to update I will copy or clone the original Instance to a new one.
Then change the validTo date from the cloned value to date.now and saveChanges.
thats all :-)
My code is
But I got an error message:
Auf ein Entitätsobjekt kann nicht von mehreren Instanzen von IEntityChangeTracker verwiesen werden.
Any Idieas ?
thank you
Public Sub GenVersion(entitySetName As String, OrigEntity As Object, Action As String) Dim key As EntityKey Using context As New BDEntities() Dim SCDObject As Object = OrigEntity key = context.CreateEntityKey(entitySetName, SCDObject) SCDObject.ValidTo = Date.Now SCDObject.Version = 100 context.AddObject(entitySetName, SCDObject) context.SaveChanges() End Using End Sub
Thursday, February 7, 2013 9:28 AM
Answers
-
Hi WinidDancer,
Welcome to the MSDN forum.
According to your code, it seems you just create a reference typeof Object instead of creating a new entity object. Thus, when you add this object to ObjectSet, actually, the original entity object is added. I guess the original entity object has not been detached from original context, so you will get the exception. I recommend you try this:
class Program {//This simple sample only copy the scalar properties. static void Main(string[] args) { using (var context = new Entities()) { var p = context.CompanyProducts.First(); GenVersion<CompanyProduct>("CompanyProducts", p); } } public static void GenVersion<T>(String entitySetName, T OrigEntity) where T : EntityObject { using (var context = new Entities()) { T t = context.CreateObject<T>(); foreach (PropertyInfo PI in OrigEntity.GetType().GetProperties()) { if (PI.PropertyType != typeof(EntityKey) && PI.PropertyType != typeof(EntityState) && PI.PropertyType.IsGenericType == false && PI.GetCustomAttributes(typeof(EdmRelationshipNavigationPropertyAttribute), false).Count() == 0) { Console.WriteLine(PI.Name); PI.SetValue(t, PI.GetValue(OrigEntity, null), null); } } context.AddObject(entitySetName, t); context.SaveChanges(); } } }
If you want to complete copy an entity object, please check this link:http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/631e7699-f1a1-4b1a-bf35-f47935c2e04a
Best Regards,
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Proposed as answer by Glenn CondronMicrosoft employee Monday, February 18, 2013 11:47 PM
- Marked as answer by Alexander Sun Thursday, February 21, 2013 8:01 AM
Friday, February 8, 2013 9:19 AM -
I think Alexanders code is just showing you a way to clone the object, rather than telling you to detach. His code uses reflection to loop over each property of the original object and sets the value on the new property to the value of the old.
The problem you have here is really the same as cloning a normal object, as far as I know EF will not provide any help for cloning. So you should clone the entity the same way you would clone any object, and then add the new object to EF. Updating the old one at the same time probably.
The code you showed at the start doesn't create a new copy of the object, it just uses the one you passed in.
EDIT: You should be able to find a C# to VB converter online that will translate the samples provided from C# to VB for you if it makes it easier for you.
We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.
- Edited by Glenn CondronMicrosoft employee Wednesday, February 13, 2013 11:40 PM
- Proposed as answer by Glenn CondronMicrosoft employee Monday, February 18, 2013 11:47 PM
- Marked as answer by Alexander Sun Thursday, February 21, 2013 8:01 AM
Wednesday, February 13, 2013 11:39 PM
All replies
-
Hi WinidDancer,
Welcome to the MSDN forum.
According to your code, it seems you just create a reference typeof Object instead of creating a new entity object. Thus, when you add this object to ObjectSet, actually, the original entity object is added. I guess the original entity object has not been detached from original context, so you will get the exception. I recommend you try this:
class Program {//This simple sample only copy the scalar properties. static void Main(string[] args) { using (var context = new Entities()) { var p = context.CompanyProducts.First(); GenVersion<CompanyProduct>("CompanyProducts", p); } } public static void GenVersion<T>(String entitySetName, T OrigEntity) where T : EntityObject { using (var context = new Entities()) { T t = context.CreateObject<T>(); foreach (PropertyInfo PI in OrigEntity.GetType().GetProperties()) { if (PI.PropertyType != typeof(EntityKey) && PI.PropertyType != typeof(EntityState) && PI.PropertyType.IsGenericType == false && PI.GetCustomAttributes(typeof(EdmRelationshipNavigationPropertyAttribute), false).Count() == 0) { Console.WriteLine(PI.Name); PI.SetValue(t, PI.GetValue(OrigEntity, null), null); } } context.AddObject(entitySetName, t); context.SaveChanges(); } } }
If you want to complete copy an entity object, please check this link:http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/631e7699-f1a1-4b1a-bf35-f47935c2e04a
Best Regards,
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Proposed as answer by Glenn CondronMicrosoft employee Monday, February 18, 2013 11:47 PM
- Marked as answer by Alexander Sun Thursday, February 21, 2013 8:01 AM
Friday, February 8, 2013 9:19 AM -
Thank you very much for you solution.
You say the problem is that I have detach the original object first ?
My last VB version was 6.0 and I started now with 2010 a lot aof changes ;-). And C# is not my prefered language.
Thank a lot
Friday, February 8, 2013 2:25 PM -
I think Alexanders code is just showing you a way to clone the object, rather than telling you to detach. His code uses reflection to loop over each property of the original object and sets the value on the new property to the value of the old.
The problem you have here is really the same as cloning a normal object, as far as I know EF will not provide any help for cloning. So you should clone the entity the same way you would clone any object, and then add the new object to EF. Updating the old one at the same time probably.
The code you showed at the start doesn't create a new copy of the object, it just uses the one you passed in.
EDIT: You should be able to find a C# to VB converter online that will translate the samples provided from C# to VB for you if it makes it easier for you.
We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.
- Edited by Glenn CondronMicrosoft employee Wednesday, February 13, 2013 11:40 PM
- Proposed as answer by Glenn CondronMicrosoft employee Monday, February 18, 2013 11:47 PM
- Marked as answer by Alexander Sun Thursday, February 21, 2013 8:01 AM
Wednesday, February 13, 2013 11:39 PM