[英]Entity framework with many one-to-zero-or-one relationships
我想在一个实体中实现许多一对零或一的关系,但是在使它工作然后为它生成迁移方面遇到问题。
public class Invoice
{
public int Id { get; set; }
public int? MorningExpenseId { get; set; }
public int? EveningExpenseId { get; set; }
public Expense MorningExpense { get; set; }
public Expense EveningExpense { get; set; }
}
public class Expense
{
public int Id { get; set; }
public int InvoiceId { get; set; }
public Invoice Invoice { get; set; }
}
modelBuilder.Entity<Invoice>()
.HasOptional<Expense>(p => p.MorningExpense)
.WithRequired(g => g.Invoice);
modelBuilder.Entity<Invoice>()
.HasOptional<Expense>(p => p.EveningExpense)
.WithRequired(g => g.Invoice);
但是我收到一个错误的Schema specified is not valid. Errors: The relationship '...' was not loaded because the type '...' is not available.
的错误Schema specified is not valid. Errors: The relationship '...' was not loaded because the type '...' is not available.
Schema specified is not valid. Errors: The relationship '...' was not loaded because the type '...' is not available.
。 我还尝试在“ Expense”类中使用主组合键,例如:
public enum ExpenseType { Morning, Evening };
public class Expense
{
public int Id { get; set; }
public ExpenseType ExpenseType { get; set; }
public Invoice Invoice { get; set; }
}
但要使其正常工作也就没有运气。 应该如何使用Fluent API来实现?
在实体框架中,应用程序类型必须与数据库类型匹配。 关系必须具有虚拟键。
您必须像这样编码
public class Invoice
{
public int Id { get; set; }
public int MorningExpenseId { get; set; }
public int EveningExpenseId { get; set; }
public virtual Expense MorningExpense { get; set; }
public virtual Expense EveningExpense { get; set; }
}
public class Expense
{
public int Id { get; set; }
public int InvoiceId { get; set; }
public virtual Invoice Invoice { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.