Print Page | Close Window

Entity without primary key

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1097
Printed Date: 22-Sep-2025 at 4:11pm


Topic: Entity without primary key
Posted By: rclarke
Subject: Entity without primary key
Date Posted: 19-Feb-2009 at 2:13pm
I'm converting a MS Access application to SQL server using DevForce EF. Part of my development process involves importing Access tables into the SQL database and then converting the Access table date to the new equivilent SQL Server table. During one such import and subsequent conversion I tried to query the Access Table with the following code:

Dim startDate As DateTime = Convert.ToDateTime(Me._uxTicketStartDateDateEdit.DateTime)

        Dim accessTicketQuery = _
        AEM.Mgr.AccessTicket1.Where(Function(t) t.Ticket_Date.Value >= startDate.Date)
        _ticketsFromAccess.ReplaceRange(accessTicketQuery)

        If _ticketsFromAccess.Count = 0 Then
            Logger("Query produced no tickets")
            Return
        Else
            Logger(String.Format("Query produced {0} tickets", _ticketsFromAccess.Count))
        End If

The query should have returned over 4000 records but only 3 were returned. Upon further examination I found that the imported table did not have a primary key defined so I set the approptiate column in the table as a primary key and the query workes as expected. The full set of records is returned. I know that every table should have a PK but in this case this table will be used only once and deleted. I'm not sure if this is an EF issue or and Devforce issue.

Thanks,

Ron




Replies:
Posted By: kimj
Date Posted: 20-Feb-2009 at 3:35pm
The Entity Framework, or at least the Entity Data Model Designer, won't allow you to define an Entity without an EntityKey.  When you try this the designer gives you an error and the code is not generated.  However, when you added the Entity to the model using the designer, it arbitrarily marked some properties as part of the EntityKey when it didn't find a primary key in the table.  I guess I shouldn't say this is arbitrary, but I don't actually know what criteria it's using.  If you look in the generated entity model you'll see a message like:
"warning 6002: The table/view 'XX' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view."
So, you likely had an Entity defined with an "unusual" EntityKey.  DevForce then used this EntityKey when the query was run, with unpredictable results.
 
The restriction that an Entity have an EntityKey is both an EF and DevForce EF requirement.  EF won't generate its model, and even should this somehow succeed DevForce also insists that every entity have "identity" in the form of a unique EntityKey.  This doesn't mean that you must define a primary key on the table, but you must define an EntityKey on the Entity.  To do this correctly though requires hand editing the EDMX file to ensure that the storage and conceptual models agree, so defining a PK in the database is probably the easiest way to resolve the issue.



Print Page | Close Window