DiPix
DiPix

Reputation: 6083

Should every table have a primary key using ADO.NET?

I'm using database-first approach with Oracle. One of my table doesn't have a primary key. It only has 2 columns which are foreign keys of other tables.

I have generated model in ASP.NET MVC project from database (Add - New Item - ADO.NET Entity Data Model).

But there is a problem - I get an error:

Error 159: EntityType 'DbModel.Store.SomeTableWithoutPK' has no key defined. Define the key for this EntityType. E:\Git_repo\ZZ\ZZ.Domain\DAL\DbModel.edmx

Does this mean that each table must have a primary key? Can I avoid this? Or I will be forced to add new column with a primary key to this table? Of course there is also possibility to apply primary key to multiple column, but is it necessary?

Upvotes: 0

Views: 264

Answers (1)

Mike
Mike

Reputation: 1667

Every table should have a primary key for database efficiency and so that you can edit records.

You don't need to create a new column for the primary key in your 2 column table

In designer, select both columns and use both together as the primary key. As long as nulls are not allowed and there are no duplicates you should be OK.

Since this is a many to many table and you are using EF, you may find later that adding a datetime column to the table with getdate() as the default value will make data maintenance easier

Upvotes: 1

Related Questions