EF 更新子实体

EF Update sub entity

我有以下 classes

#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 class,但是作为 ProductParameters 子项的 ProductCategory class 不能

上下文代码

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,我尝试了在互联网上找到的各种东西

如果您想更新 ProductCategory table 的 Id,那么这是不可能的,因为它是主键和 Identity 列。在这种情况下,如果您需要更新该列,则在 table 中获取一个新列(如 ProductCategoryCode)并创建该主键和标识列并从 Id 列中删除约束。