0

I have 3 classes as you can see :

public class car
{
        public int Id { set; get; }
        public string Name { set; get; }
        public List<CarTest> CarTest { get; set; }

        public car(string name, int id, List<CarTest> carTests)
        {
            Id = id;
            Name = name;
           
            CarTest = carTests;
        }

        public car()
        {
        }
}

public class CarTest
{
      public OVTest OV { get; set; }
      public string CarOV { get; set; }
}

public class OVTest
{
      public string Name { get; set; }
}

I want to edit my value as you can see :

static void Main(string[] args)
{
    myctx myc = new myctx();
    
    var cartests = new List<CarTest>();
    var cartest = new CarTest();

    var OV = new OVTest();
    OV.Name = "editedName";
    cartest.OV = OV;
    cartest.CarOV = "2OV";
    cartests.Add(cartest);
    var editcar = new car("ee5155fe",2, cartests);
    myc.ChangeTracker.TrackGraph(editcar, e =>e.Entry.State = EntityState.Modified);

    myc.SaveChanges();

    Console.ReadLine();
}

But I get this error :

System.InvalidOperationException: 'The property 'CarTestId' on entity type 'OVTest' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.'

This is my config:

public class CarConfig : IEntityTypeConfiguration<car>
{
        public void Configure(EntityTypeBuilder<car> builder)
        {     
            builder.OwnsMany(u => u.CarTest);
            builder.OwnsMany(u => u.CarTest).OwnsOne(c=>c.OV);
        }
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Ehsan Akbar
  • 5,728
  • 15
  • 72
  • 137
  • Make following change From : var OV = new OVTest(); OV.Name = "editedName"; To : var OV = new OVTest() {Name = "editedName"}; – jdweng Aug 02 '20 at 09:43
  • @jdweng I changed it ,but i get same error System.InvalidOperationException: 'The property 'CarTestId' on entity type 'OVTest' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.' – Ehsan Akbar Aug 02 '20 at 09:46
  • You should be able to add new OVTest with a key value in the constructor. You just can't change value after the object has been constructed. – jdweng Aug 02 '20 at 09:56
  • You are trying to set state `Modify` to an entity that has just being created. Change it to `Added`. – Alvin Stefanus Aug 02 '20 at 10:01
  • @AlvinStefanus I change it to added ,i get this error :Cannot insert explicit value for identity column in table 'cars' when IDENTITY_INSERT is set to OFF. – Ehsan Akbar Aug 02 '20 at 10:03
  • You are setting the `IDENTITY_INSERT` off which means, you cannot set the `Id` of your `car` manually. So remove the `Id` from your constructor `public car( string name, List carTests)`. See this if you want to set your `Id` manually https://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity – Alvin Stefanus Aug 02 '20 at 10:11
  • @AlvinStefanus I want to edit the car with id=2 – Ehsan Akbar Aug 02 '20 at 10:13
  • @AlvinStefanus I don't want to add value ,i want to edit – Ehsan Akbar Aug 02 '20 at 10:14
  • This code: `var editcar = new car("ee5155fe",2, cartests);` means that you are creating a new entity, if you want to edit it you should be doing this: `var editcar = db.cars.where(m => m.Id == 2).FirstOrDefault();`. You need to get the entity from the database, then modify its property, then set state to modified, then save. – Alvin Stefanus Aug 02 '20 at 10:16

0 Answers0