This is a migrated thread and some comments may be shown as answers.

One-to-Many Associations insertion problem

1 Answer 47 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ajoua taha
Top achievements
Rank 1
ajoua taha asked on 25 Oct 2016, 02:48 PM

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

 

1 Answer, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 02 Nov 2016, 08:57 AM
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.
Tags
General Discussions
Asked by
ajoua taha
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Share this question
or