none
Entity Framework 4.1 - Cannot rename autogenerated Foreign Key, new Column being Added instead RRS feed

  • Question

  • Hi,

    Dev Env: Visual Studio 2010, EF 4.1

    My problem:

    When I add a 1 to many association between two entities in EDMX designer (right click on designer -> add association -> checkbox 'Add foreign key properties to entity' checked), a foreign key is generated for me, which is great. However, if I rename the foreign key to anything other than the autogenerated one, a new column is added in my table.

    The only other related post I can find on the forum is this: Why does EF 4.1 Generate extra foreign keys and how do you stop it? (http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/8d7dcc15-c095-469a-8981-b18eb0920542?prof=required)

    I've followed what's been suggested in the post, but it didn't really work for me.

    Has anyone else encountered this problem before and what's your solution?

    Thanks!


    C#/.NET beginner


    • Edited by Icedin Monday, October 15, 2012 11:18 AM
    Monday, October 15, 2012 11:17 AM

Answers

  • Hi Allen,

    Thanks for your help. Yes, I have tried that as well - creating foreign key by hand,in other words,

    1. add a new field and called it something silly 'IamAField'

    2. add an association between two entities

    3. map the dependent property to 'IamAField'

    4. generate database

    5. A new column is added to the table with default field name

    I know this is hard to reproduce. We can use the default property names here, although that's not ideal...

    Thanks

    Monday, October 22, 2012 10:24 AM

All replies

  • Hi Icedin,

    Welcome to MSDN Forum.

    Commonly, after add a relationship, we can select the foreign key and modify its name in Properties Window. I've tested and the foreign key name modified successfully, below are my test steps.

    1. Add 'EntityOne';

    2. Add 'EntityTwo';

    3. Add a one to many relationship between them;

    4. Select the foreign key in 'EntityTwo' and modify the name in 'Properties Window';

    5. Generate database from model.

    The foreign key generated in the database has the correct name.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 16, 2012 2:50 AM
    Moderator
  • Thanks AI,

    I also tried it myself, for simple database schema, what you suggested always worked. However, if you have a complex schema with different entities linking to each other, for whatever reason, the renamed foreign key is disregarded and a new key is created (a new column in database table). For example, if the autogenerated foreign key is called 'Product_ProductId', and I rename it to 'ProductId', in my table, it will have two columns 'Product_ProductId' and 'ProductId', with 'ProductId' on longer being used as the foreign key. The only way I could get this working is to not rename the foreign key at all! The other way is for me to generate the entity relations from the database, but it almost defeats the purpose of EDMX designer.

    I don't know if anyone else has seen this problem or if the entity framework team is aware of this, I have also looked at the SQL script it generates, there's no mentioning of 'Product_ProductId' whatsoever. There could be some steps I have missed, but it shouldn't be so difficult to rename a key, should it!? It's very frustrating, I spent a few hours and couldn't find any solutions, honestly the more I use EF, the more I dislike about it.

    Thanks

    Wednesday, October 17, 2012 9:07 AM
  • Hi Icedin,

    Please try this: When you create the assosiation, don't let EF help you to generate the FK. Then add a property to your dependent entity and named it as what you want. Finally, double click the relationship, a 'Referential Constraint' window will appear, set the 'Principle' as your independent entity, and set 'Dependent Property' as the property which you added manually before. Finally click 'Ok'.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 18, 2012 3:26 AM
    Moderator
  • Hi Icedin,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 22, 2012 2:41 AM
    Moderator
  • Hi Allen,

    Thanks for your help. Yes, I have tried that as well - creating foreign key by hand,in other words,

    1. add a new field and called it something silly 'IamAField'

    2. add an association between two entities

    3. map the dependent property to 'IamAField'

    4. generate database

    5. A new column is added to the table with default field name

    I know this is hard to reproduce. We can use the default property names here, although that's not ideal...

    Thanks

    Monday, October 22, 2012 10:24 AM