繁体   English   中英

我是否需要使用EntityFramework更新一对多关系中的两个实体?

[英]Do I need to update both Entities in a one-to-many relationship using EntityFramework?

我有两个看起来像这样的实体:

public class Account
{
    public int AccountId { get; set; }
    public string AccountName { get; set; }
    public decimal AccountBalance { get; set; }

    public virtual Expense Expense { get; set;} 
}

public class Expense
{
    public int ExpenseId { get; set; }
    public string ExpenseName { get; set; }
    public decimal ExpenseAmount { get; set; }

    public virtual ICollection Accounts { get; }
}

如果要使用已存储在DatabaseAccount对象向DbContext.Expenses添加新的Expense ,是否还需要将新的Expense添加到该Account Entity

在我的测试中,如果我只是创建一个新的Expense实体,则无法从Account Model访问关联的Expenses

请执行下列操作:

  • 将新Expense添加到费用收集
  • 在已经存在的Account实体上,将Expense属性设置为创建的新实体。
  • 保存更改。

暂无
暂无

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

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