实体框架核心自动生成的GUID

编程入门 行业动态 更新时间:2024-10-26 20:21:26
本文介绍了实体框架核心自动生成的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", Name = "Mubeen", Password = "123123" }, new User() { Email = "Tahir@gmail", Name = "Tahir", Password = "321321" }, new User() { Email = "Cheema@gmail", 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

本文发布于:2023-11-14 16:39:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动生成   实体   框架   核心   GUID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!