One-to-Many Association Fluent API duplicate entry upon insert/save

2 posts, 0 answers
  1. Steven
    Steven avatar
    3 posts
    Member since:
    Feb 2015

    Posted 21 Jul 2015 Link to this post

    I am using the Telerik DataAccess Fluent API Version: 2015.2.624.1 and SQLite

    In my code I have one Customer model that can have many Account models associated. So I believe I've got the same basic setup as is found here:

    http://docs.telerik.com/data-access/developers-guide/code-only-mapping/mapping-clr-types-properties-and-associations/mapping-associations/fluent-mapping-mapping-clr-mapping-associations-one-to-many​

    Here is the mapping I use to create the association:

                accountMapping
                    .HasAssociation(a => a.Customer)
                    .WithOpposite(c => c.CustomerAccounts)
                    .HasConstraint((a, c) => a.CustomerID == c.CustomerID);

    And here is the code I have that saves a new customer and associated account:

                     using (FluentModel db = new FluentModel())
                    {
                        customer.CustomerAccounts.Add(account);
                        db.Add(customer);
                        db.SaveChanges();
                    }

    This results in two rows being inserted into the customer table. Seems customer gets inserted first then another duplicate customer inserts and is associated with the added account model/entity.

    If I write it like this:

                        account.Customer = customer;
                        db.Add(account);
                        db.Add(customer);
                        db.SaveChanges();​

    Then I get the expected results one customer, one account. But is there any way to get the first way to work without the duplicate entry?

    Please let me know if you need more information or if I'm missing something here. Thanks!

     

  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 24 Jul 2015 Link to this post

    Hello Steven,

    Unfortunately, I was not able to reproduce the issue in our test environment using Visual Studio 2013, SQLiteFactory Version=1.0.92.0 and Telerik DataAccess 2015.2.624.1

    The observed behavior with this setup and the code snippet which you had sent is that in the database is inserted one Customer and one Account which is persisted by reachability and the CustomerId column in the Account table is not populated.

    If you want to populate the Foreign Key column when a Child entity is added in the children collection in the Parent class then you should allow Telerik DataAccess to manage the association. In these documentation articles you could find more information about How Telerik DataAccess manage the navigation properties.

    You should know that there is a mismatch of association behavior according to on which end of the association is the definition. In your case if you want to use IsManaged, I will recommend you to change the side on which the association is defined from accountMapping to customerMapping:
    1.customerMapping
    2.    .HasAssociation(c => c.CustomerAccounts)
    3.    .WithOpposite(a => a.Customer)
    4.    .HasConstraint((c, a) => c.CustomerID == a.CustomerID)
    5.    .IsManaged();

    Thus you will have the behavior that you want to achieve.

    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
     
    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