GUID COMB strategy in EF

后端 未结 3 406
误落风尘
误落风尘 2020-11-28 12:58

Is there any way to implement the Guid COMB identity strategy for objects in the new Entity Framework 4.1 using the CodeFirst design? I thought setting the StoreGenera

3条回答
  •  醉酒成梦
    2020-11-28 13:37

    Why worry about defaults for Guid columns in the database at all? Why not just generate the Guid on the client like any other value. That requires you have a method in your client code that will generate COMB-like guids:

    public static Guid NewGuid()
    {
        var guidBinary = new byte[16];
        Array.Copy( Guid.NewGuid().ToByteArray(), 0, guidBinary, 0, 8 );
        Array.Copy( BitConverter.GetBytes( DateTime.Now.Ticks ), 0, guidBinary, 8, 8 );
        return new Guid( guidBinary );
    }
    

    One of the advantages of the Guid is specifically that you can generate them on the client without a round trip to the database.

提交回复
热议问题