EF 更新子实体
Posted
技术标签:
【中文标题】EF 更新子实体【英文标题】:EF Update sub entity 【发布时间】:2021-10-28 11:22:50 【问题描述】:我有以下课程
#1
public class Product
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string wId get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Code get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Name get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Ean get; protected set;
[Column(TypeName = "DECIMAL(10,2)")]
public decimal Price get; protected set;
[Column(TypeName = "INT")]
public int Vat get; protected set;
[Column(TypeName = "TINYINT")]
public bool Stockable get; protected set;
[Column(TypeName = "TINYINT")]
public bool Produced get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Unit get; protected set;
[Column(TypeName = "DECIMAL(10,2)")]
public decimal Netto get; protected set;
[Column(TypeName = "DECIMAL(10,2)")]
public decimal Brutto get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Description get; protected set;
[Column(TypeName = "DECIMAL(10,2)")]
public decimal QuantityGlobal get; protected set;
[Column(TypeName = "TINYINT")]
public bool HalfProduct get; protected set;
[Column(TypeName = "DATETIME")]
public DateTime CreatedAt get; protected set;
[Column(TypeName = "DATETIME")]
public DateTime UpdatedAt get; protected set;
public ProductParameters ProductParameters get; set;
[Column(TypeName = "INT")]
public int CompanyId get; protected set;
#2
public class ProductParameters
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Size get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Width get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Length get; protected set;
[Column(TypeName = "INT")]
public ProductCategory Category get; protected set;
[Column(TypeName = "INT")]
public ProductPattern Pattern get; protected set;
public ProductParameters()
#3
public class ProductCategory
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string Name get; protected set;
[Column(TypeName = "NVARCHAR(MAX)")]
public string NamePL get; protected set;
我可以用 EF 更新 ProductParameter 和 Product 类,但是 ProductParameters 的子类 ProductCategory 不能
上下文代码
try
var oldproduct = testcontext.Product.Where(x => x.Id == product.Id).Include(x => x.ProductParameters).Include(p => p.ProductParameters.Category).Include(k => k.ProductParameters.Pattern).FirstOrDefault();
if (oldproduct != null)
testcontext.Entry(oldproduct).CurrentValues.SetValues(product);
testcontext.Entry(oldproduct.ProductParameters).CurrentValues.SetValues(product.ProductParameters);
testcontext.Entry(oldproduct.ProductParameters.Category).CurrentValues.SetValues(product.ProductParameters.Category)
testcontext.SaveChanges();
return oldproduct;
else
return null;
catch(Exception ex)
throw new Exception(ex.Message);
我如何才能真正更新它?我只需要设置 ID,无需创建新模型。 现在我知道如何更新 Product 和 ProductParameters,我尝试了在互联网上找到的各种方法
【问题讨论】:
请展示你到目前为止所做的事情。你需要更新什么?请显示视图。以及为视图创建模型的操作。我从未见过包含父模型和所有嵌套模型的视图。并显示更新操作。 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。 【参考方案1】:如果您想更新ProductCategory
表的Id
,那么这是不可能的,因为它是主键和Identity
列。在这种情况下,如果您需要更新该列,则在表中获取一个新列(如 ProductCategoryCode
)并创建该主键和标识列,并从 Id
列中删除约束。
【讨论】:
以上是关于EF 更新子实体的主要内容,如果未能解决你的问题,请参考以下文章