1

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 !

Flash_Back
  • 565
  • 3
  • 8
  • 31

2 Answers2

2

In model designer get properties of Primary key. Find StoreGeneratedPattern and change it from None to Identity. save everything and now Primary key will be generated correctly.

Hamed Nikzad
  • 618
  • 8
  • 14
0

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.

TcKs
  • 25,849
  • 11
  • 66
  • 104
  • I don't really need sequence identifiers but according to the posts I read `newid()` will be inserted anywhere in the table while `newsequentialid()` will always be added to the end of the table, so the performance of inserts is improved. I'm ok using `int` but then how can I maintain a foreign key between `aspnet_Users`' id (which is Guid) and my custom User table (if I change its primary key from Guid to int) ? – Flash_Back Apr 13 '15 at 15:15
  • It depends on what is your CLUSTER INDEX. The table is not simple list. The physical storage is made with b-tree. In fact the inserting records in different parts of table can make INSERTs faster. All is dependent on your CLUSTER INDEX and their fill-factor. However I don't think you will have so much new users to need optimization in INSERTs. It's really fast. – TcKs Apr 13 '15 at 15:23
  • The cluster index is the primary key (I've read that this is the default behaviour and I did not change it), that's to say the Guid. I have to admit that I'm not so familiar with cluster indexes but you are actually right, I don't really need to optimize INSERTs. However I need great performances for searches. Won't `newid()` lower it at all ? – Flash_Back Apr 13 '15 at 15:32
  • Are you sure, the insert will be bottlenect? Because, there is really big chance you are trying micoroptimize part of code which will be fast. There is big chance you application code will be much slower than inserting new record. – TcKs Apr 13 '15 at 15:38
  • Actually I am not, that's just what I read (http://stackoverflow.com/questions/1587185/newid-vs-newsequentialid-what-are-the-differences-pros-and-cons), I'm quite new and learning about best practises and ways to optimize performances. Therefore I just read posts discussing this issue, saying that `newsequentialid()` was more suitable regarding fragmentation, but it actually may be quite unnecessary in my case. If it only impacts INSERTs and not searches I guess I can keep `newid()`. – Flash_Back Apr 13 '15 at 15:54
  • There are some special cases, where you need sequential IDs and IDs must be Guid. But it's very rare. – TcKs Apr 13 '15 at 16:05
  • I can't replace `Guid` to `int` regarding `aspnet_Users` primary key issue, but for the moment don't have a specification regarding sequentials IDs. Thus I will go back to `newid()` and see if I have real fragmentation issues later :) Anyway thanks for your time and your answers ! – Flash_Back Apr 13 '15 at 16:11