Change of Identity Not Supported

5 posts, 2 answers
  1. Abdullah
    Abdullah avatar
    11 posts
    Member since:
    Oct 2011

    Posted 16 Feb 2012 Link to this post

    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.
  2. Answer
    Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 17 Feb 2012 Link to this post

    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 >>
  3. DevCraft banner
  4. Abdullah
    Abdullah avatar
    11 posts
    Member since:
    Oct 2011

    Posted 17 Feb 2012 Link to this post

    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?
  5. Answer
    Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 17 Feb 2012 Link to this post

    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 >>
  6. Abdullah
    Abdullah avatar
    11 posts
    Member since:
    Oct 2011

    Posted 17 Feb 2012 Link to this post

    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);

Back to Top
DevCraft banner