HichemSeeSharp
HichemSeeSharp

Reputation: 3318

How generate Guid from entity framework when object created

I have bound a datagrid to a context and add some rows at runtime but it generates an exception and couldn't save changes due to property of type Guid generates only empty Guid.
How generate Guid from entity framework when object created?

Upvotes: 0

Views: 5044

Answers (3)

Gray
Gray

Reputation: 1

You can just change the property 'StoreGeneratedPattern' for the field to Identity.

So your Object 'SalesOrderHeader' has a field 'rowguid'. Just select the field and go to properties.

Select 'StoreGeneratedPattern' and choose 'Identity'.

I should point out that I am also new to Entity Framework but this worked for me. I just hope that when the model is updated that this setting is retained. It would be a pain if it wasn't.

Thanks, Gray.

Upvotes: 0

Ali Adlavaran
Ali Adlavaran

Reputation: 3735

In other way, You can make a partial class for YourEntity like this:

public partial class YourEntity
{
   public Guid ID { get; set;}    
   public YourEntity()
   {
      ID = Guid.NewGuid();
   }
}

Upvotes: 0

mfanto
mfanto

Reputation: 14418

You can decorate your Guid property with [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

This will generate a new row id every time you insert.

Upvotes: 3

Related Questions