簡體   English   中英

實體框架代碼首先使用不同項目中的模型

[英]Entity Framework Code first with models in a different project

晚上,我相信這可能很簡單......但我做錯了。

我正在嘗試使用3個(現在的)項目創建一個解決方案:

  1. MCV Web App
  2. 楷模
  3. DAL(實體框架)

我想將模型保存在UI和DAL的不同項目/程序集中,因為模型可以在項目之間重用,我們可能想要更換DAL等而不必弄亂模型等...

無論如何,解決問題。

我在我的模型項目中創建了一些類,如:

public class Client
{
    public int ClientID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
    public string Notes { get; set; }
    public virtual Contact BillingContact { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }

    public virtual ICollection<Invoice> Invoices { get; set; }
}

然后我在DAL中創建了一個DBContext

using System.Data.Entity;
using DBDS.Invoice.Models;

namespace DBDS.Invoice.DAL.EF
{
    public class InvoiceDataContext : DbContext
    {
        public DbSet<Client> Clients;
    }
}

我已經在DAL項目上啟用了遷移,添加了一個遷移並調用了update-database - 數據庫已經創建但沒有表。

無論如何,我確定我是密集的 - 任何幫助將不勝感激。

謝謝

Clients需要成為實體框架的一個屬性才能獲取它

public class InvoiceDataContext : DbContext
{
    public DbSet<Client> Clients { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM