Entity Framework And Tables With No Primary Keys

OK, I think we can all agree that having tables without primary keys is a bad idea.  However, sometimes we are forced to use backend databases that have multiple design flaws.  In fact, database design flaws are one of the very things the Entity Framework (EF) can fix.  EF allows us to fix issues we find in a database without actually effecting the database schema at all.

Anyway, yesterday I added a table to my EF model that had no primary key.  I was surprised to learn that EF decided that since there was not a primary key it would just use all the non-nullable columns as a concatenated primary key.  This might not be what you want.

To fix the issue, I had to hack the XML and remove the keys from the table definition.  I was not able to fix it using the UI in Visual Studio 2008 SP1.  Instead, I had to right click the model file in the solution explorer and select "Open With...".   From there select the XML Editor.  Then you just need to find the table definition and remove the unwanted columns from the primary keys section.  It's a bit of a pain, but it is the only fix I know of at the time.

Let me know if you find a better solution.

Posted in Labels: , |

4 comments:

  1. Anonymous Says:

    You should be able to right click on the entity columns and uncheck the Is Key property.

    Agree though it's more tedious than it should be!

  2. Anonymous Says:

    Hey there Ben - I had no idea that this is the case. At least you can deselect the "Is Key" setting as David says but this is just amazingly bad form. I've responded to your post here: http://www.sqlskills.com/BLOGS/KIMBERLY/post/Seriously-are-you-kidding-me.aspx.

    All I have to say is WOW.
    kt

  3. Shimmy Says:

    Post an issue to microsoft, you can save others from having this troubles in next releases:

    https://connect.microsoft.com/VisualStudio/content/content.aspx?ContentID=9790

  4. Anonymous Says:

    Does anyone know where I can find free online grant applications?

    rH3uYcBX