locked
Why code first approach migration is adding records where it should update RRS feed

  • Question

  • User-1317453444 posted

    Issue: Below code has added the 6 records properly. But when I try to update the Name field with value "Hotels" in object with the "Hospitality" then migration is adding the record instead of updating the record.

    Code snippet:

    PropertyType toolHome = new PropertyType() { Id = SequentialGuid.NewGuid(), Name = "Homes", Path = @"App_Data\HomeTool.xlsx", CreatedOn = DateTime.Now, NoOfTabs = 2, DisplayOrder = 1 };
    PropertyType toolHotel = new PropertyType() { Id = SequentialGuid.NewGuid(), Name = "Hotels", Path = @"App_Data\HotelTool.xlsx", CreatedOn = DateTime.Now, NoOfTabs = 3, DisplayOrder = 2 };
    PropertyType toolRetail = new PropertyType() { Id = SequentialGuid.NewGuid(), Name = "Retail", Path = @"App_Data\RetailTool.xlsx", CreatedOn = DateTime.Now, NoOfTabs = 3, DisplayOrder = 3 };
    PropertyType toolOffice = new PropertyType() { Id = SequentialGuid.NewGuid(), Name = "Offices", Path = @"App_Data\OfficeTool.xlsx", CreatedOn = DateTime.Now, NoOfTabs = 3, DisplayOrder = 4 };
    PropertyType toolHospital = new PropertyType() { Id = SequentialGuid.NewGuid(), Name = "Hospitals", Path = @"App_Data\HospitalTool.xlsx", CreatedOn = DateTime.Now, NoOfTabs = 3, DisplayOrder = 5 };
    PropertyType toolEducation = new PropertyType() { Id = SequentialGuid.NewGuid(), Name = "Education", Path = @"App_Data\EducationTool.xlsx", CreatedOn = DateTime.Now, NoOfTabs = 3, DisplayOrder = 6 };

    _ctx.ProjectType.AddOrUpdate(c => new { c.Name }, toolHome);
    _ctx.ProjectType.AddOrUpdate(c => new { c.Name }, toolHotel);
    _ctx.ProjectType.AddOrUpdate(c => new { c.Name }, toolRetail);
    _ctx.ProjectType.AddOrUpdate(c => new { c.Name }, toolOffice);
    _ctx.ProjectType.AddOrUpdate(c => new { c.Name }, toolHospital);
    _ctx.ProjectType.AddOrUpdate(c => new { c.Name }, toolEducation);

    _ctx.SaveChanges();

    Id Name Path CreatedOn UpdatedOn NoOfTabs CreatedBy UpdatedBy DisplayOrder
    CD8FBFC1-9202-4014-82F7-A36B00E066FE Retail App_Data\RetailTool.xlsx 10:22.2 NULL 3 NULL NULL 3
    6B0CDC7E-BD30-4D8A-8338-A36B00E066FE Hospitals App_Data\HospitalTool.xlsx 10:22.2 NULL 3 NULL NULL 5
    AE27A45B-FB9E-4F2D-9BC9-A36B00E066FE Homes App_Data\HomeTool.xlsx 10:22.2 NULL 2 NULL NULL 1
    1C0FD517-9C2C-4A7E-9F45-A36B00E066FE Offices App_Data\OfficeTool.xlsx 10:22.2 NULL 3 NULL NULL 4
    0E595AE7-6636-4A6D-B778-A36B00E066FE Hotels App_Data\HotelTool.xlsx 57:05.6 NULL 3 NULL NULL 2
    7FF8A69B-D6DF-4266-8E46-A83A0106FDCA Education App_Data\EducationTool.xlsx 10:22.2 NULL 3 NULL NULL 6

    Let me know where I need to change the code to fix the issue.

    Tuesday, August 28, 2018 6:06 PM

All replies

  • User1520731567 posted

    Hi nelsonrock101,

    As I can see in your code,

    PropertyType toolHome = new PropertyType() { Id = SequentialGuid.NewGuid(), Name = "Homes", Path = @"App_Data\HomeTool.xlsx", CreatedOn = DateTime.Now, NoOfTabs = 2, DisplayOrder = 1 };

    Your Id is Guid,so it can't find the row in db with the same Id.

    I suggest that you could modify the code like below,

    For example:

     Tool toolHome = new Tool() {Id= new Guid("7178a7fd-51ab-e811-b867-8cec4b594df1"),  Name = "Homes23",  Path = @"App_Data\HomeTool.xlsx" , CreatedOn = DateTime.Now};
                                //give Id a clear value
    
     context.Tools.AddOrUpdate(c => c.Id, toolHome);//Id instead of Name

    How it works:

    Best Regards.

    Yuki Tao

    Wednesday, August 29, 2018 7:19 AM
  • User-1317453444 posted

    Please see to the below things:

    • See the Id cannot be static as it is not right approach for the dynamic application.
    • Reason why 'Name' cannot work as due to first point I cant use Id?

    Let me know the resolution

    Wednesday, August 29, 2018 11:40 AM
  • User1520731567 posted

    Hi nelsonrock101,

    Please see to the below things:

    • See the Id cannot be static as it is not right approach for the dynamic application.
    • Reason why 'Name' cannot work as due to first point I cant use Id?

    Oh,yes.You could also use other fields,such as: Name.

    For example(Based on previous examples),

     Tool toolHome2 = new Tool() {Id = SequentialGuid.NewGuid(),Name = "Homes23", Path = @"App_Data\HomeTool22222.xlsx", CreatedOn = DateTime.Now };
    
     context.Tools.AddOrUpdate(c => c.Name, toolHome2);

    It means compiler will search the record whose Name ='Homes23',and then find the difference(Path) and update.

    Best Regards.

    Yuki Tao

    Thursday, August 30, 2018 2:03 AM
  • User-1317453444 posted

    Its not working as I have requirement to update 'Name' and not 'Path'

    Thursday, September 6, 2018 9:27 AM
  • User1520731567 posted

    Its not working as I have requirement to update 'Name' and not 'Path'

    You need to find a field that is unique and clear its value,so that Compiler can find the record and update in db.

    context.Tools.AddOrUpdate(c => c.XXX, toolHome);
    Friday, September 7, 2018 9:46 AM