我想在一個實體中實現許多一對一或一對一的關係,但是我有問題讓它工作,然後生成遷移它。具有許多一對一或一對一關係的實體框架
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.
。 我還與在'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實現?
伊夫不需要使用可空 '公衆詮釋的? MorningExpenseId {get;組; }' 'public int? EveningExpenseId {get;組; }' 而不是使用我的例子從我的git頁面 '公共虛擬列表關鍵字{get;組; }' 'public virtual ApplicationUser User {get;組; }' 這將使用虛擬關鍵字將實體鏈接到彼此 –
TinMan7757
我只需要評論......從數據庫的角度來看,無論身處何處都使用「Id」,這是不好的。我鼓勵你用名稱:ExpenseId,InvoiceId等來命名錶中的Id列。 – ErikE