none
relation one to one RRS feed

  • Question

  • Hi,

    I have the classes --> A, B, C, D and E

    A, B and C inheritance of D.

    A and B have a relation with E (one to one).

    I don't want that A and B have the foreing key to E.

    I need another table "TableAssociation" that has a relation to E and D, is possible? I don't want to create another class I would rather do changes only in mapping.

    I'm using Entity Framework v6 and Fluent API.

    thanks

    Wednesday, May 7, 2014 6:25 PM

Answers

  • Hello,

    >>A and B have a relation with E (one to one). I don't want that A and B have the foreing key to E.

    One to one relationship in code first is based on their primary keys, we do not need to have the forrign keys.

    For a detail example, you can check this link:

    http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

    It uses the fluent API as:

    modelBuilder.Entity<StudentAddress>()
    
                        .HasRequired(e => e.Student)
    
                        .WithRequiredDependent(s => s.StudentAddress);

    >>I need another table "TableAssociation" that has a relation to E and D, is possible? I don't want to create another class I would rather do changes only in mapping.

    What relationship does table "TableAssociation" has to E and D? Entity Framework will generate a junction table for many to many relationship, under many to many scenario, we do not need to create another class. If you want E and D have a many to many relationship, you can check this example using fluent API:

    http://msdn.microsoft.com/en-us/data/jj591620.aspx#ManyToMany

    If you want the table “TableAssocaition” has a separate relationship with both E and D, it is impossible without creating the entity class.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 8, 2014 6:30 AM
    Moderator