主键值EF核心

问题描述 投票:0回答:1

我需要获取主键值,然后才能由EF Core将其分配给模型。

我的模特:

**namespace Tree.Models
{
  public class MeterLocationTree
  {
    [Key]
    public int Id { get; set; }
    [Required]
    public string LocationElement { get; set; }
    [Required]
    public int[] Path { get; set; }
  }
}**

我的模型迁移快照:

modelBuilder.Entity("Tree.Models.MeterLocationTree", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("integer")
                        .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);

                    b.Property<string>("LocationElement")
                        .IsRequired()
                        .HasColumnType("text");

                    b.Property<int[]>("Path")
                        .IsRequired()
                        .HasColumnType("integer[]");

                    b.HasKey("Id");

                    b.ToTable("MeterLocationTree");
                });

是否可以在“添加”操作期间将当前主键值分配给模型之前获取它?那么也许我们可以使用主键值生成的ef机制?

postgresql entity-framework primary-key
1个回答
0
投票

模型包含数据,在将其分配给EF核心中的模型之前,我们无法访问该值。

© www.soinside.com 2019 - 2024. All rights reserved.