简体   繁体   English

来自同一类/模型的其他字段的实体框架更新字段

[英]Entity Framework update field from other fields of the same class/model

public class Person
{
     public string FirstName { get; set; } = string.Empty
     public string LastName { get; set; } = string.Empty
     public string FullName { get; set; } = string.Empty
}

How can I update FullName as如何将FullName更新为

FullName = $"{FirstName} {LastName}"

Placing the above line in the constructor doesn't seem to cut it as values of FirstName & LastName seems to be default and Entity Framework persists as an empty string too.将上面的行放在构造函数中似乎并没有削减它,因为FirstNameLastName值似乎是默认值,并且实体框架也保留为空字符串。

How can I make this work with EF without explicitly calling a method or assigning directly to the field FullName from other classes?如何在不显式调用方法或从其他类直接分配给字段FullName情况下使用 EF 进行此操作?

Package: EF Core Cosmos 3.1包:EF Core Cosmos 3.1

If you don't need to persist the FullName property, you can go with:如果您不需要保留 FullName 属性,则可以使用:

[NotMapped]
public string FullName => $"{FirstName} {LastName}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM