我正在使用 EF 4.4 + Code First。
我试图将 ChartofAccount 实体添加到 DBContext 并填充公司的密钥,但它要求我在验证时也填写公司的 CompanyName。我以为 DBContext 会为我查找关联公司,而不是尝试添加新公司?
public class ChartofAccount: MasterData
{
public ChartofAccount()
{
Company = new Company();
Category1 = new AccountCatagory();
Category2 = new AccountCatagory();
}
[Key]
[Required]
public string Code { get; set; }
public virtual Company Company { get; set; }
[Required]
public string AccountName { get; set; }
[Required]
[StringLength(3)]
public string AccountCurrency { get; set; }
public virtual AccountCatagory Category1 { get; set; }
public virtual AccountCatagory Category2 { get; set; }
public string Reference { get; set; }
public bool HasTransaction { get; set; }
}
public class Company : MasterData
{
[Key]
public int Id { get; set; }
[Required]
public string CompanyName { get; set; }
[Required]
DateTime CurrentAccountPeriod { get; set; }
[Required]
[StringLength(3)]
public string BaseCurrencyCode { get; set; }
}