factory methods when entity has primary key as an identity column

Posts   
 
    
kirsten
User
Posts: 5
Joined: 04-Jun-2011
# Posted on: 05-Jun-2011 07:11:04   

Hi I am wondering if I have used a wrong setting somewhere in generating this code. my ContactID field is an identity key in SQL Server, and meant to be assigned by the database when the insert statement is run. However the generated code is passing the value in. Am I doing something wrong? I am using LLBLGen Pro v3.1 , EF4, SQLExpress2008R2 Thanks Kirsten

/// <summary>Factory method to create a new instance of the entity type 'Crm.Contact'</summary> /// <param name="contactIdValue">The initial value for the field 'ContactId'</param> /// <param name="firstNameValue">The initial value for the field 'FirstName'</param> /// <param name="lastNameValue">The initial value for the field 'LastName'</param>

public static Contact CreateContact(System.Int32 contactIdValue, System.String firstNameValue, System.String lastNameValue) { var toReturn = new Contact(); toReturn.ContactId = contactIdValue; toReturn.FirstName = firstNameValue; toReturn.LastName = lastNameValue; return toReturn; }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Jun-2011 05:07:45   

Does that field has assigned a Sequence in LLBLGen Designer. You can see that if you edit the entity and then go to 'Field mappings' sub-tab.

Even so, if the sequence (auto-identity in SQLServer) is present in DB, the database should ignore that value and insert the next id. Remember that LLBLGen Pro just generate the code so you can use EF right away. I don't know how it works if you set a value to a sequence field. Maybe EF tries to force the value. Can you clarify this a bit?

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 06-Jun-2011 11:04:51   

.. it also could be a small issue in our template. simple_smile Indeed, please confirm the field is indeed marked as a sequenced field in the designer.

Frans Bouma | Lead developer LLBLGen Pro
kirsten
User
Posts: 5
Joined: 04-Jun-2011
# Posted on: 06-Jun-2011 21:35:24   

The sequence is SCOPE_IDENTITY() Thanks Kirsten

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Jun-2011 13:05:51   

The sequence is SCOPE_IDENTITY()

So you found SCOPE_IDENTITY set to that field in LLBLGen Pro Designer, correct? Could you re-generate the code and try again, please.

my ContactID field is an identity key in SQL Server, and meant to be assigned by the database when the insert statement is run. However the generated code is passing the value in.

How did you know that the value was passed to the database in an INSERT statement? Did you get an exception or what?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 09-Jun-2011 11:18:29   

When you pass '0' it should work. Could you try that for me please?

The template indeed doesn't filter out readonly fields, it only filters out optional fields for the signature. So it looks like a small bug in our template. We'll fix it, so the readonly fields aren't passed to the factory anymore. The problem is that if 0 passing works, we can't fix it for v3.0/1, as that would mean a breaking change (signature changes). If 0 passing doesn't work, we can fix it today, as no-one will use it that way anyway. (signatures of factory methods with sequenced pk fields are changing, the rest isn't).

We added the method as the code generated by the EF designer contains it too, however the method itself is not really needed to use the code.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 13-Jun-2011 11:05:12   

Changing this would make the method not usable for creating an entity for attachment to a context for updates, as EF requires that because it doesn't do change tracking inside the entities. So we'll keep it as-is

Frans Bouma | Lead developer LLBLGen Pro