none
Problem with EntityFramework 4 RRS feed

  • Question

  • Hi there,

    I'm creating an Entity that has 4 key properties and I need to create a relationship with another Entity.

    What I do when I've tables with more than one key row is to create an extra row and create an index without duplicates so I create the relationship between this table and any other using only this extra row. It works perfect with DataSets but now with EntityFramework 4 I can't do that because the "Referential constraint" dialog automatically add the key rows in the "Principal key".

    Is there a way to achieve the same result with EntityFramework as I get with the DataSets?

    Thank you.

    PD: The data store is SQL Server.

    • Moved by Andreas Johansson Monday, July 18, 2011 9:05 AM More suitable forum (From:Visual C# General)
    Friday, July 15, 2011 2:29 PM

Answers

  • Thank you for posting.

    First, I suggest you can go through Entity Framework Overview. http://msdn.microsoft.com/en-us/library/bb399567.aspx

    In addition, if there are some relationships/associations between two tables in the database, EDM can also map this relationships/associataions in CSDL,SSDL and C/S mapping file. If you want to modify relationships in EDM after you have finished, you should re-load these entities from your database and re-map them.

    Please feel free to let me know if you have any question.

    Best Regards,


    Larcolais Gong[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.

    Tuesday, July 19, 2011 8:34 AM

All replies

  • I've had relation with multiple columns between entities, though I imported the model from database first. are you modeling in the entity designer?

    Friday, July 15, 2011 2:53 PM
  • Hi Andreas,

    If I import the model from the database using the approach I said before when I compile the app I get an error saying that I've to complete the relationship between the two Entities because it seems like the EntityFramework always use all the key properties (rows) to create the relation between two tables and this is what I wanna avoid.

     

    The question is: there's a way to create the relationship in the way I explained before???

    Friday, July 15, 2011 3:00 PM
  • Your original question wasn't entirely clear. When you say you have 4 "key" columns, do you mean that it's a multi-part key where all 4 columns together make up the key (which is what Andreas is referring to) or do you mean that there are 4 different columns, any of which may be used as a key?

    If it's the latter, I don't believe EF will work very well for that. I have run into similar problems in the past with legacy databases. If you have control of this database, having multiple independent keys is an indicator of bad database design - the best option is to revisit the database and normalize it. If you have no control over the database and just have to use it, you might just switch to old fashioned LINQ to SQL. When a bad database design is forced on you, LINQ to SQL is a little more forgiving than EF.


    Check out My Blog for tech news, development tips, and other information for geeks like me.
    Friday, July 15, 2011 5:14 PM
  • Hi Tim,

    > When you say you have 4 "key" columns, do you mean that it's a multi-part key where all 4 columns together make up the key

     

    Yes, it's a multi-part key in the table (entity) and I did the database.

     

    Because of that when I wanna create a relationship with another Entity I've to create the 4 columns in the other Entity, in that way EF works OK. But what I want is to avoid creating the 4 columns in the other Entity.

    Can I do that in EF?

    Thank you

    Friday, July 15, 2011 6:32 PM
  • Saturday, July 16, 2011 12:11 PM
  • Thank you for posting.

    First, I suggest you can go through Entity Framework Overview. http://msdn.microsoft.com/en-us/library/bb399567.aspx

    In addition, if there are some relationships/associations between two tables in the database, EDM can also map this relationships/associataions in CSDL,SSDL and C/S mapping file. If you want to modify relationships in EDM after you have finished, you should re-load these entities from your database and re-map them.

    Please feel free to let me know if you have any question.

    Best Regards,


    Larcolais Gong[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.

    Tuesday, July 19, 2011 8:34 AM