实体框架核心自动生成的GUID [英] Entity Framework Core Auto Generated guid

查看:112
本文介绍了实体框架核心自动生成的GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指导我吗,我想要一个表 primeryKey 作为 guid ,该表在插入时具有db生成的值.

Can some One guide me I want primeryKey of a table as guid having db generated value on insert.

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

但是它给出了error

无法添加实体类型用户"的种子实体,因为没有为必需的属性"Id"提供值.

The seed entity for entity type 'User' cannot be added because there was no value provided for the required property 'Id'.

这是我的实际模型类和DbContxt类:

Here is my actual model classes and DbContxt class:

public class BaseModel
{
     [Key]
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public Guid Id { get; set; }

     [Required]
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public DateTime CreatedOn { get; set; } = DateTime.UtcNow;


     [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
     public DateTime? UpdatedOn { get; set; }

     [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
     public DateTime LastAccessed { get; set; }
 }

 public class User : BaseModel
 {
        [Required]
        [MinLength(3)]
        public string Name { get; set; }

        [Required]
        [MinLength(3)]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        [MinLength(6)]
        public string Password { get; set; }
  }

然后在MyDbContext中:

Then in the MyDbContext:

public class MyDbContext: DbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder mb)
        {
            base.OnModelCreating(mb);
            
            mb.Entity<User>().HasData(
                new User() { Email = "Mubeen@gmail.com", Name = "Mubeen", Password = "123123" },
                new User() { Email = "Tahir@gmail.com", Name = "Tahir", Password = "321321" },
                new User() { Email = "Cheema@gmail.com", Name = "Cheema", Password = "123321" }
                );
        }

        public DbSet<User> User { get; set; }
    }

请帮助!

推荐答案

您遇到的问题并非专门针对自动生成的Guid.对于任何自动生成的键值,包括常用的自动增量(标识)列,也会发生同样的情况.

The problem you are experiencing is not specific for autogenerated Guids. The same happens for any autogenerated key values, including the commonly used auto increment (identity) columns.

这是由特定的数据种子引起的( HasData)要求:

It's caused by a specific Data Seeding (HasData) requirement:

此类种子数据由迁移管理,并且需要在不连接数据库的情况下生成用于更新数据库中已有数据的脚本.这施加了一些限制:

This type of seed data is managed by migrations and the script to update the data that's already in the database needs to be generated without connecting to the database. This imposes some restrictions:

  • 即使通常由数据库生成,也需要指定主键值.它将用于检测迁移之间的数据更改.
  • 如果以任何方式更改主键,则先前的种子数据将被删除.

注意第一个项目符号.因此,对于普通CRUD,您的PK将自动生成,但使用HasData流利API时,您需要指定,并且该值必须为常数(不变),因此您不能使用Guid.NewGuid().因此,您需要生成几个Guid,使用它们的字符串表示形式,并使用类似以下的内容:

Note the first bullet. So while for normal CRUD your PK will be auto generated, you are required to specify it when using HasData fluent API, and the value must be constant (not changing), so you can't use Guid.NewGuid(). So you need to generate several Guids, take their string representation and use something like this:

mb.Entity<User>().HasData(
    new User() { Id = new Guid("pre generated value 1"), ... },
    new User() { Id = new Guid("pre generated value 2"), ... },
    new User() { Id = new Guid("pre generated value 3"), ... }
    );

这篇关于实体框架核心自动生成的GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆