Nano Taboada
Nano Taboada

Reputation: 4182

Should I necessarily have a PK on the source table to create an ADO.NET Entity Data Model?

I'm trying to "Entitify" some external table (which I don't administer) in order to use it within a MVC application and in principle I'm not being terribly successful with the attempt (VS2008 output):

Error List [0 Errors] [0 Warnings] [1 Message]

Description

The table/view 'DATABASE.dbo.table' does not have a primary key defined and no valid primary key could be inferred.

This table/view has been excluded. To use the entity you will need to review your schema, add the correct keys and uncomment it.

File

C:\Documents and Settings\%USERNAME%\My Documents\Visual Studio 2008\Projects\MVC_Entity_Test\MVC_Entity_Test\Models\EmployeesDataModel.edmx

Line

0

Column

1

Project

MVC_Entity_Test

Output

Show output from: Entity Data Model

Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
The model was generated with warnings or errors.
Please see the Error List for more details. These issues must be fixed before
running your application.
Loading metadata from database took 00:00:06.2809306.
Generating model took 00:00:03.0359078.
Writing out the EDMX file took 00:00:00.0230083.
Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
The model was generated with warnings or errors.
Please see the Error List for more details. These issues must be fixed before running
your application.
Loading metadata from database took 00:00:12.3208290.
Generating model took 00:00:03.6914563.
Writing out the EDMX file took 00:00:02.1670689.

So my question would be, is it absolutely mandatory to declare a PK on the source table to correctly map it as an ASP.NET Entity?

Note: I wouldn't like this thread to become a dissertation about great, standard-driven, academically-backed database modeling, thing is, I have to deal with this external table that comes from a view somewhere, that comes from a table somewhere, so on so so forth, so I don't really have any control over it. The point being that I just want to get my side of the thing done.

Upvotes: 1

Views: 3741

Answers (2)

LanceSc
LanceSc

Reputation: 2124

The Entity Framework is a mapping framework, so it needs a way to uniquely map every row to an object. This requires some form of a unique identifier, which is used to generate SQL DML statements so the row can be modified.

In your situation if there is a combination of columns do not contain NULLs that will uniquely identify a row, you can manually add the necessary information to the edmx file. This MSDN page will describe the basics of manually adding an object to an edmx, http://msdn.microsoft.com/en-us/library/bb399785.aspx. Remember it is just an XML file that you can edit.

Upvotes: 6

marc_s
marc_s

Reputation: 754668

Well, I would generally recommend it as a best practice to put a primary key on any table - except in very rare cases like a table used for bulkloading data etc.

After all, only with the primary key can you uniquely identify and keep apart rows in the table (and thus entities or object instances in your domain model).

One SQL guru even said: If it doesn't have a primary key, it's not a table! :-)

Marc

Upvotes: 0

Related Questions