我在实体中像这样配置的SQL Server中有一个计算主键:
var e = builder.Entity<MyEntity>();
e.ToTable("MyTable", "MySchema")
.HasKey(k => k.Id);
e.Property(k => k.Id)
.ValueGeneratedOnAdd();
e.Property(k => k.Id)
.Metadata.BeforeSaveBehavior = PropertySaveBehavior.Ignore;
e.Ignore(i => i.PreviousId);
在EF Core 2.1中保存更改后如何更新属性?
myEntity.RelatedProperty = 1;
_db.SaveChangesAsync();
// Here I want to have my updated Id
更新: 这是表的定义:
CREATE TABLE [MySchema][MyTable](
[Id] AS (isnull((CONVERT([nvarchar],[RelatedProperty],(0))+'-')+CONVERT([nvarchar],[RelatedProperty2],(0)),'-')) PERSISTED NOT NULL,
[RelatedProperty] [int] NOT NULL,
[RelatedProperty2] [int] NOT NULL
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
答案 0 :(得分:0)
您没有。不支持更改PK。如果您认为这是个问题,请等到开始尝试在外键关系中使用它们。