One-to-Many Associations insertion problem

2 posts, 0 answers
  1. ajoua taha
    ajoua taha avatar
    25 posts
    Member since:
    Feb 2016

    Posted 25 Oct Link to this post

    I followed the instructions in the tutorial "How to: Manage One-to-Many Associations":

    categoryConfiguration.HasAssociation(x => x.Cars).
        HasFieldName("_cars").WithOpposite(x => x.Category).
        ToColumn("CategoryID").HasConstraint((y, x) =>  x.CategoryID == y.CategoryID ).
        IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    carConfiguration.HasAssociation(x => x.Category).
        HasFieldName("_category").WithOpposite(x => x.Cars).
        ToColumn("CategoryID").HasConstraint((x, y) =>  x.CategoryID == y.CategoryID ).
        IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);

    using (FluentModel dbContext = new FluentModel())
    {
        Car newCar = new Car();
        newCar.Make = "Audi";
        newCar.Model = "A8";
        Category newCategory = new Category();
        newCategory.CategoryName = "MyCategory";
        dbContext.Add(newCategory);
        newCategory.Cars.Add(newCar);
        dbContext.SaveChanges();
    }

     

    but when i want to add more than one car to newCategory.Cars : 

     

    using (FluentModel dbContext = new FluentModel())
    {
        Car newCar1 = new Car();
        newCar1.Make = "Audi";
        newCar1.Model = "A8";
        Car newCar2 = new Car();
        newCar2.Make = "Volkswagen";
        newCar2.Model = "Passat";
        Category newCategory = new Category();
        newCategory.CategoryName = "MyCategory";
        dbContext.Add(newCategory);
        newCategory.Cars.Add(newCar1);
        newCategory.Cars.Add(newCar2);
        dbContext.SaveChanges();
    }

    Only one Car is saved !!

    Help please

     

  2. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 02 Nov Link to this post

    Hello ajoua taha,

    I tested the provided configuration with the SofiaCarRental database, as structured here, and I was unable to reproduce the behaviour you describe. The tests I did include the following scenarios:
    - The two code snippets one directly after the other, like this:
    public static void Main(string[] args)
    {
        using (FluentModel dbContext = new FluentModel())
        {
            dbContext.Log = Console.Out;
            Car newCar = new Car();
            newCar.Make = "Audi";
            newCar.Model = "A8";
            newCar.TagNumber = "Mytagnumber";
            Category newCategory = new Category();
            newCategory.CategoryName = "MyCategory";
            dbContext.Add(newCategory);
            newCategory.Cars.Add(newCar);
            dbContext.SaveChanges();
        }
        using (FluentModel dbContext = new FluentModel())
        {
            dbContext.Log = Console.Out;
            Car newCar1 = new Car();
            newCar1.Make = "Audi";
            newCar1.Model = "A8";
            newCar1.TagNumber = "Mytagnumber";
            Car newCar2 = new Car();
            newCar2.Make = "Volkswagen";
            newCar2.Model = "Passat";
            newCar2.TagNumber = "Mytagnumber2";
            Category newCategory = new Category();
            newCategory.CategoryName = "MyCategory";
            dbContext.Add(newCategory);
            newCategory.Cars.Add(newCar1);
            newCategory.Cars.Add(newCar2);
            dbContext.SaveChanges();
        }
    }
    As a result the second call to SaveChanges() fails with DuplicateKeyException (Insert of '6140637-' failed: Telerik.OpenAccess.RT.sql.SQLException: Violation of UNIQUE KEY constraint 'IX_Cars_TagNumber'. Cannot insert duplicate key in object 'dbo.Cars'. The duplicate key value is (Mytagnumber).). In the database I have only the category and the car from the first call to SaveChanges().

    - The two code snippets in the order above but this time respecting the unique constraint for the Tag number (lines 9 and 22 below):
    01.public static void Main(string[] args)
    02.{
    03.    using (SofiaCarRentalContext dbContext = new SofiaCarRentalContext())
    04.    {
    05.        dbContext.Log = Console.Out;
    06.        Car newCar = new Car();
    07.        newCar.Make = "Audi";
    08.        newCar.Model = "A8";
    09.        newCar.TagNumber = "Mytagnumber";
    10.        Category newCategory = new Category();
    11.        newCategory.CategoryName = "MyCategory";
    12.        dbContext.Add(newCategory);
    13.        newCategory.Cars.Add(newCar);
    14.        dbContext.SaveChanges();
    15.    }
    16.    using (SofiaCarRentalContext dbContext = new SofiaCarRentalContext())
    17.    {
    18.        dbContext.Log = Console.Out;
    19.        Car newCar1 = new Car();
    20.        newCar1.Make = "Audi";
    21.        newCar1.Model = "A8";
    22.        newCar1.TagNumber = "Mytagnumber1";
    23.        Car newCar2 = new Car();
    24.        newCar2.Make = "Volkswagen";
    25.        newCar2.Model = "Passat";
    26.        newCar2.TagNumber = "Mytagnumber2";
    27.        Category newCategory = new Category();
    28.        newCategory.CategoryName = "MyCategory";
    29.        dbContext.Add(newCategory);
    30.        newCategory.Cars.Add(newCar1);
    31.        newCategory.Cars.Add(newCar2);
    32.        dbContext.SaveChanges();
    33.    }
    34.}
    As a result I successfully inserted the two categories and the three cars.

    To troubleshoot the situation further on your side, I ask you to provide me with the following details:
    - Does the unique constraint for Tag number exist in the database on your side?
    - How do you consume the model (through the first code snippet that calls SaveChanges(), or through both code snippets that consume SaveChanges())?

    I'm looking forward to your feedback.

    Regards,
    Doroteya
    Telerik by Progress
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
Back to Top