[英]Update entity on db with a property of list of entities with one-to-many relationship
[英]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; }
}
如果要使用已存储在Database
的Account
对象向DbContext.Expenses
添加新的Expense
,是否还需要将新的Expense
添加到该Account Entity
?
在我的测试中,如果我只是创建一个新的Expense
实体,则无法从Account Model
访问关联的Expenses
。
请执行下列操作:
Expense
添加到费用收集 Account
实体上,将Expense
属性设置为创建的新实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.