主键值 EF Core
primary key value EF Core
我需要先获取一个主键值,然后 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机制?
模型包含数据,在将值分配给 EF 核心中的模型之前我们无法访问该值。
我需要先获取一个主键值,然后 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机制?
模型包含数据,在将值分配给 EF 核心中的模型之前我们无法访问该值。