none
Creating an association between two entities RRS feed

  • Question

  • In DB we have tables MyTable1 and MyTable2 and no relationship exist between them. In EF we have an option to create an association between the two entities Entity1 ( mapped to MyTable1 ) and Entity2 ( mapped to MyTable2 ). 

    a) I know it's kind of a vague question,but still ... is there a situation where it would make sense to create an association ( either an independent or foreign key association ) between Entity1 and Entity2, even though the MyTable1 and MyTable2 have no relationship?

    b) When creating an association between Entity1 and Entity2, we are also presented with "Add foreign key properties to Entity2" option, which adds a foreign key property to Entity2. But why would we want to add a foreign key property to Entity2, if we can't map this property to any of MyTable2 columns?

    thank you

    Thursday, November 10, 2011 8:17 PM

Answers

  • Hi,

    I think this may be the answers you are looking for:

    A) The EF modelling tool supports two scenarios, Model-First and Database-First. The ability to create associations between two entities in the modelling tool is there to be able to update/create a database from the model in a Model-First aspect by using the Generate Database from Model choice in the context menu.

    While it is possible to create a relationship between two unrelated tables the code will not compile by doing it in a Database-First scenario, and thus creating a manual association that isn't mapped to any database foreign key in the store isn't possible.

    I have no idea what the "someone on a another forum" talks about regarding view <-> table, do you have any link to that post?

    B) See answer A, for a Model-First environment someone may want to have a column in their entities that is a Id column of the association, other does not. This checkbox is there for the conceptual model. The storage model will have a foreign key column in the background.

    I hope that this answered your questions?


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by KlemS100 Tuesday, November 15, 2011 11:36 PM
    Tuesday, November 15, 2011 9:39 PM

All replies

  • Hi,

    you you have two tables that dont have any relationship, why you think about to create one? I dont get this at all. In EF you dont have the "option" to create the association. EF supports existing associations in your model. If there is no semantic relationship, it makes no sense to create foreign keys between tables, that have nothing in common.

    If this doesnt answer your question, try to explain your thoughts a bit more specific :)

    Greets

    Holger

    Thursday, November 10, 2011 8:59 PM
  • Hi,

    you you have two tables that dont have any relationship, why you think about to create one? I dont get this at all. In EF you dont have the "option" to create the association. EF supports existing associations in your model. If there is no semantic relationship, it makes no sense to create foreign keys between tables, that have nothing in common.

    If this doesnt answer your question, try to explain your thoughts a bit more specific :)

    Greets

    Holger

    For example, what if I want to add an association between a table entity and a view entity, where view entity is a subset of a table?! It would be nice if I could show that the two entities are related 
    Friday, November 11, 2011 5:43 PM
  • Hi KlemS100,

    There is no key for View entity. So it doesn't work.

    I'm not sure if this is what you want: http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting-part-ii-adding-inheritance/

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 14, 2011 9:17 AM
    Moderator
  • "There is no key for View entity."

    I know views don't have primary keys, but doesn't Data Model wizard constuct a composite key from all the non-nullable values in the view-based entity?

    "So it doesn't work."

    Anyways...reason for my confusion is that on another forum someone said that it is possible to create an association between a table entity and a view entity, but offered no further insight into how it is done. So you're saying that the guy is wrong and thus EF supports only existing associations between the entities?  

    "I'm not sure if this is what you want: http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting-part-ii-adding-inheritance/"

    Not really. Reason I'm asking this question is just a curiosity of how associations work under the covers ... thus I'm not looking for a solution to a particular design problem  

    • Marked as answer by KlemS100 Tuesday, November 15, 2011 11:35 PM
    • Unmarked as answer by KlemS100 Tuesday, November 15, 2011 11:36 PM
    Tuesday, November 15, 2011 8:33 PM
  • Hi,

    I think this may be the answers you are looking for:

    A) The EF modelling tool supports two scenarios, Model-First and Database-First. The ability to create associations between two entities in the modelling tool is there to be able to update/create a database from the model in a Model-First aspect by using the Generate Database from Model choice in the context menu.

    While it is possible to create a relationship between two unrelated tables the code will not compile by doing it in a Database-First scenario, and thus creating a manual association that isn't mapped to any database foreign key in the store isn't possible.

    I have no idea what the "someone on a another forum" talks about regarding view <-> table, do you have any link to that post?

    B) See answer A, for a Model-First environment someone may want to have a column in their entities that is a Id column of the association, other does not. This checkbox is there for the conceptual model. The storage model will have a foreign key column in the background.

    I hope that this answered your questions?


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by KlemS100 Tuesday, November 15, 2011 11:36 PM
    Tuesday, November 15, 2011 9:39 PM
  •  

    "I have no idea what the "someone on a another forum" talks about regarding view <-> table, do you have any link to that post?"

    http://stackoverflow.com/questions/8085452/creating-an-association-between-two-entities

     

    "I hope that this answered your questions?"

    yep, it sure did :)

     

    thanx a million for helping me with this

    Tuesday, November 15, 2011 11:35 PM