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!