I am trying to use "Table Splitting" in EF Code First and it works fine when I use one of the entities relationships of type one-to-many, but as soon as I use it in a many-to-many relationship I start getting this error:
> (27,6) : error 3018: Problem in mapping fragments starting at line
> 27:Foreign key constraint 'Itinerary_Addresses_Target' from table
> ItineraryAddress (Address_Id) to table User (Id): The columns of table
> ItineraryAddress are mapped to AssociationSet Itinerary_Addresses's
> End Itinerary_Addresses_Target but the key columns of table User are
> not mapped to the keys of the EntitySet Addresses corresponding to
> this End.
Here is the code (https://github.com/jorgef/tablesplitting):
**Table Splitting**
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public User User { get; set; }
}
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Address>().ToTable("Users");
modelBuilder.Entity<User>().HasRequired(u => u.Address).WithRequiredPrincipal(a => a.User);
**One-To-Many Relationship**
public class Itinerary
{
public int Id { get; set; }
public ICollection<Address> Addresses { get; set; }
}
**With the previous code, everything works like a charm, the problem is when introducing a many-to-many relationship**
**Many-To-Many Relationship**
public class Address
{
...
public ICollection<Itinerary> Itineraries { get; set; }
}
After adding that relationship, the app raises the mentioned exception on runtime. I managed to save to disk the generated dbmx just in case that helps, here is the link: https://github.com/jorgef/tablesplitting/blob/master/TableSplitting/SavedModel.edmx
If somebody wants to play with the two versions of the app, the one working and the one not working, I have two different commits:
- Table splitting working with one to many relationship
- Table splitting not working with many to many relationship
**Any ideas or thougths are appreciated.**
Many thanks!
Comments: Hi, Although this problem seems to be fixed for the provided scenario, I found another case where it doesn't work. I have provided all the details in the bug #1611: https://entityframework.codeplex.com/workitem/1611 I have also updated the git repository (https://github.com/jorgef/tablesplitting) with the new scenario so you can reproduce it easily. Thanks!
> (27,6) : error 3018: Problem in mapping fragments starting at line
> 27:Foreign key constraint 'Itinerary_Addresses_Target' from table
> ItineraryAddress (Address_Id) to table User (Id): The columns of table
> ItineraryAddress are mapped to AssociationSet Itinerary_Addresses's
> End Itinerary_Addresses_Target but the key columns of table User are
> not mapped to the keys of the EntitySet Addresses corresponding to
> this End.
Here is the code (https://github.com/jorgef/tablesplitting):
**Table Splitting**
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public User User { get; set; }
}
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Address>().ToTable("Users");
modelBuilder.Entity<User>().HasRequired(u => u.Address).WithRequiredPrincipal(a => a.User);
**One-To-Many Relationship**
public class Itinerary
{
public int Id { get; set; }
public ICollection<Address> Addresses { get; set; }
}
**With the previous code, everything works like a charm, the problem is when introducing a many-to-many relationship**
**Many-To-Many Relationship**
public class Address
{
...
public ICollection<Itinerary> Itineraries { get; set; }
}
After adding that relationship, the app raises the mentioned exception on runtime. I managed to save to disk the generated dbmx just in case that helps, here is the link: https://github.com/jorgef/tablesplitting/blob/master/TableSplitting/SavedModel.edmx
If somebody wants to play with the two versions of the app, the one working and the one not working, I have two different commits:
- Table splitting working with one to many relationship
- Table splitting not working with many to many relationship
**Any ideas or thougths are appreciated.**
Many thanks!
Comments: Hi, Although this problem seems to be fixed for the provided scenario, I found another case where it doesn't work. I have provided all the details in the bug #1611: https://entityframework.codeplex.com/workitem/1611 I have also updated the git repository (https://github.com/jorgef/tablesplitting) with the new scenario so you can reproduce it easily. Thanks!