GUID COMB strategy in EF

后端 未结 3 407
误落风尘
误落风尘 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条回答
  •  旧时难觅i
    2020-11-28 13:43

    The simplest answer

    public class User
    {
        public User(Guid? id = null, DateTime? created = null)
        {
            if (id != null)
                Id = id;
    
            if (created != null)
                Created = created;
        }
    
        public User()
        {
        }
    
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public DateTime? Created { get; internal set; }
    
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid? Id { get; internal set; }
    }
    

    This assumes you have your database table set with the default of newsequentialid() which in my case is managed by FluentMigrator migrations.

提交回复
热议问题