Do I have to have a PK in the source table to create the ADO.NET Entity Data Model?
I am trying to "Entitify" some external table (which I am not administering) to use in an MVC application and in principle I am not terribly successful with trying (VS2008 exit):
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
Exit
Show Result: 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 is, is it absolutely necessary that the PK in the source table correctly render it as an ASP.NET object?
Note. ... I would hate to see this thread become a dissertation on big, standardized, academically supported database modeling, the point is that I have to deal with this external table that comes from somewhere, somewhere, from somewhere - then, from somewhere like that, so on, so I don't really control it. The thing is, I just want my side to be done.
a source to share
Entity Framework is a rendering framework, so it needs a way to uniquely map each row to an object. This requires some form of unique identifier that is used to generate the SQL DML statements, so the string can be modified.
In your situation, if there is a combination of non-NULL columns that uniquely identify the row, you can manually add the required information to the edmx file. This MSDN page will cover the basics of manually adding an object to edmx, http://msdn.microsoft.com/en-us/library/bb399785.aspx . Remember, this is just an XML file that you can edit.
a source to share
Okay, I would recommend as a best practice to transfer the primary key to any table - except in very rare cases like a table used for bulk loaded data, etc.
After all, only with a primary key can you uniquely identify and store individual rows in a table (and therefore objects 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! :-)
Mark
a source to share