dev.e.loper
dev.e.loper

Reputation: 36044

Generating a newsequentialid using Linq

I'm inserting a record into a table where Id is of type uniqueidenfier. I would like that Id to be generated on SQL server, sort of like using newsequentialid.

To insert a record I'm using InsertOnSubmit method like so:

 SchedulerDataContext db = new SchedulerDataContext();
 db.Courses.InsertOnSubmit(courseDomainModel);
 db.SubmitChanges();

update: as the answer stated I need to set 'Default value or Binding' property of a column to newid(). Here is the screenshot of it for reference.

enter image description here

Upvotes: 1

Views: 430

Answers (1)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47048

From the msdn-link above

NEWSEQUENTIALID() can only be used with DEFAULT constraints on table columns of type uniqueidentifier.

When NEWSEQUENTIALID() is used in DEFAULT expressions, it cannot be combined with other scalar operators.

NEWSEQUENTIALID() cannot be referenced in queries.

That means you can not set it from LINQ, you need to put it in the table definition.

Upvotes: 2

Related Questions