0

I am building an Ntier application with EntityFramework c#. I am adding an Entity Data Model in my Data Access Layer with code-first approach from existing database.

Some of the tables of my db weren't included because they don't have primary key. I have seen some ways to work around this problem, modifying EntityFramework's edmx to force the mapping to the database, disguising some field like a key. But I am not using the .edmx, since I can't use automatic migrations with it. I only generate POCOs from my existing database and then go on with code first migrations.

Is there a way to force Entity Framework to generate a POCO for those tables without primary key ? Some only have one entry and really don't need PrimaryKey

Elekk
  • 41
  • 1
  • 1
  • 6
  • As of my experience, is quite a bad idea to have a table without primary key. you can hack arround this, but i advise against this. – Alex May 22 '17 at 14:16
  • Read more about this here: https://stackoverflow.com/questions/3996782/entity-framework-table-without-primary-key – Alex May 22 '17 at 14:18
  • Why not just convert the tables to have a PK? Working around this will be a major PITA. – spender May 22 '17 at 14:18
  • How would Entity Framework know which record to update without a primary key? Can you add a primary key? How about an indexed view? If not, you may need to use stored procedures to modify these tables. – adam0101 May 22 '17 at 14:22
  • You can add the DDL create script of the table to your migrations manually and still fool EF about the primary key property. But as said, only do this if you really, absolutely can't change the database to do things right. – Gert Arnold May 22 '17 at 15:09
  • if there is only one entry, why not just add a primary key? I think adding new column for primary key to existing tables is easier than breaking convention. Most of the time, adding new column to table will not break existing usage. – Ppp May 23 '17 at 07:50

1 Answers1

0

In the end, I just wrote my own POCOs for the tables that weren't included.

I used an attribute [KEY] above the property i wanted to act like key. I added DbSet lines in the DataModel and EF did recognize them in my database.

I didn't want to generate primary keys because my boss didn't want, and thats a reason good enough. :) Hope the best for you thx for answer

Elekk
  • 41
  • 1
  • 1
  • 6