Entity Framework auto generate GUID

后端 未结 8 1921
离开以前
离开以前 2021-02-06 21:16

I am new to EF so here goes.I have a class which contains the following

public class EmailTemplate
{
    public Guid Id { get; set; }

    [MaxLength(2000)]
             


        
8条回答
  •  一生所求
    2021-02-06 21:24

    If using .Net core then this should work for you ...

    Use fluent API

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity().Property(x => x.ID).HasDefaultValueSql("NEWID()");
    }
    

    or

    modelBuilder.Entity().Property(p => p.StudentID)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    

    Here is a more comprehensive Cheat Sheet for entity framework

提交回复
热议问题