I'm working on a website using EF 4.1 and database first.
I'm also using aspnet tables, particularly aspnet_Users
which is using Guid as a primary key.
Consequently, my custom User table also has a Guid as primary key, which is a foreign key to the aspnet_Users
id.
Recently, I read that using Guid as primary keys was a bad idea unless using newsequentialid()
.
With Sql Server Management Studio I have updated all my primary keys' default value to newsequentialid()
, and set StoreGeneratedPattern
to Identity
in my edmx designer.
However, whenever I try to add an objet (for example Item
which has a Guid primary key), its id remains empty (all 0) in database.
public void CreateItem()
{
using (var uof = new UnitOfWork())
{
Item item = new Item();
itemRepo.Add(item);
uof.Commit();
}
}
And the add method :
public class RepositoryBase<TObject> where TObject : IEntity
{
protected CavalenaEntities entities
{
get { return UnitOfWork.Current.Context; }
}
public void Add(TObject entity)
{
entities.Entry(entity).State = System.Data.EntityState.Added;
}
}
Did I forget something ?
Although I set the default value for the primary keys of my database, their Default Value
property in edmx designer is still None
. Is it normal ?
Another solution would be to use int as primary keys instead of Guid but then how could I do the link between my custom User table and aspnet_Users
, which uses Guid ?
Thanks a lot !
Using NEWID()
is absolutely OK unless you need sequence identifiers. But then you probably don't need Guid
/uniqueidentifier
, you can use int
or bigint
and identity or SQL sequence object.