This is a migrated thread and some comments may be shown as answers.
Change of Identity Not Supported
4 Answers 68 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Abdullah
Top achievements
Rank 1
Abdullah asked on 16 Feb 2012, 11:42 PM
Hello,
I want to create an entity with the same values of existing entity for a table in my database.

CarEntity car = db.Cars.Single(c => c.ID == 2);

//This line throws error, I want to create new car from existing one.
car.ID = 5;
 
db.Add(car);
db.SaveChanges();

Lets say, I want to create new car from an existing car, the table has over 20 columns, only few columns and primary key change, how can i do that, i get an error "Change of identity not supported" when i try to create a new entity  from an existing one.

4 Answers, 1 is accepted

Sort by
0
Accepted
Jan Blessenohl
Telerik team
answered on 17 Feb 2012, 09:32 AM
Hello,
The car is still managed by OpenAccess, all changes that you do are tracked and persisted back into the database. This also means that you cannot reuse the same instance as a new one. We prevent that by throwing the exception.

You have to clone the car object and add that to the database. The easiest way is to implement a copy constructor in the car class and use that.

public partial class Car
{
    public Car (Car other)
    {
        this.type = other.type;
        ...
    }
}

CarEntity carEx = db.Cars.Single(c => c.ID == 2);
CarEntity carNew = new Car(carEx);
carNew.ID = 5;
db.Add(carnEW);
db.SaveChanges();
Kind regards,
Jan Blessenohl
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Abdullah
Top achievements
Rank 1
answered on 17 Feb 2012, 11:14 AM
do i have to initialize all the properties of class in constructor, setting them one by one by hand with the values from clone class, isnt there any easy way?
0
Accepted
Ralph Waldenmaier
Telerik team
answered on 17 Feb 2012, 02:09 PM
Hi,

If you know which values you need to copy, based on your business logic, you can implement the copy process only for these properties. Otherwise you need to implement the copy process for all properties.
Alternatively, you can use reflection to copy the properties from one object to another. 
A third option, an advanced though, would be to change the T4 templates used to generate the context classes, and let the code be generated for you. Details on how to work with the templates can be found here.

I hope this information is helpful for you. Feel free to ask if you have any other question.

All the best,
Ralph
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Abdullah
Top achievements
Rank 1
answered on 17 Feb 2012, 02:54 PM
Solution:

To copy existing row  to create new one using Reflection:

public partial class Car  
{

   public Car (Car myCar)
{
        Type type = myCar.GetType();
        PropertyInfo[] properties = type.GetProperties();
 
 
         foreach (PropertyInfo property in properties)
        {
               //Choose the property to assign Value
               PropertyInfo iteratedProp = this.GetType().GetProperty(property.Name);
 
               //Initialize value to choosen property
               iteratedProp.SetValue(this, property.GetValue(myCar,null), null);
 
        }
 
 }
}



CarEntity carEx = db.Cars.Single(c => c.ID == 2);
CarEntity carNew = new Car(carEx);

Tags
General Discussions
Asked by
Abdullah
Top achievements
Rank 1
Answers by
Jan Blessenohl
Telerik team
Abdullah
Top achievements
Rank 1
Ralph Waldenmaier
Telerik team
Share this question
or