Entity Framework auto generate GUID

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

    After a long investigation, I found out that in EF Core 3.1 you need to use

    builder.Property(e => e.Id).ValueGeneratedOnAdd();
    
    0 讨论(0)
  • 2021-02-06 21:48

    Entity Framework Core Update:

    There is no need to use [DatabaseGenerated(DatabaseGeneratedOption.Identity)].
    There is no need to use fluent API

    EF Core automatically take care of it and generates Id for primary key

    Example:

    public class DummyEntity
    {
        public Guid Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
    }
    

    Seeding

        _context.DummyEntities.Add(new DummyEntity
        {
            FirstName = "Abc",
            LastName = "Def",
            Postion = "User",
        });
    
        _context.SaveChanges();
    
    0 讨论(0)
提交回复
热议问题