Entity Framework auto generate GUID

后端 未结 8 2100
离开以前
离开以前 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:43

    I prefer to leave the database to generate the id automatically for example the following schema:

    CREATE TABLE [dbo].[MyTable](
        [MyId] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Booking_BookingId]  DEFAULT (newsequentialid())
        )
    

    Then in the code first mapping I specify the following to tell Entity Framework that the database will take care of generating the value on insert.

    Property(a => a.MyId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    

提交回复
热议问题