none
EF Code First Table Splitting issue when used with many-to-many relationships RRS feed

  • Question

  • 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!

    Regards,

    Jorge Fioranelli
    http://blog.jorgef.net

    Sunday, July 14, 2013 11:13 PM

All replies

  • Hi Jorge,

    The error indicates that the keys in User table are not mapped to Addresses entity set.

    I think the problem is that you defined User as the principal end and Address as the dependent end. You can try to modify the navigation property to define the many-to-many relationship from the Address class to User class.

    public class User
    {
        ...
        public ICollection<Itinerary> Itineraries { get; set; }
    }

    Or try to make Address as the principle end.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Wednesday, July 17, 2013 8:15 AM
    Moderator
  • Hi Chester,

    Thanks for your reply.

    I tried changing the principal as follows but I get the same error:

    modelBuilder.Entity<Address>().HasRequired(a => a.User).WithRequiredPrincipal(u => u.Address).WillCascadeOnDelete(true);

    (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.

    Is it possible that the edmx generation from code first is not supporting this scenario properly?

    Or maybe I need to define something extra with the modelBuilder...

    If you open the edmx located here in VS and right click, validate, you will get the same error.

    BTW, I have created a bug in Codeplex: https://entityframework.codeplex.com/workitem/1385

    Thanks again!


    Regards,

    Jorge Fioranelli
    http://blog.jorgef.net


    • Edited by Jorge Fioranelli Wednesday, July 17, 2013 8:33 AM Adding link to bug
    Wednesday, July 17, 2013 8:29 AM