实体框架代码首先使用不同项目中的模型

时间:2014-10-27 19:38:32

标签: c# asp.net-mvc entity-framework-6

晚上, 我确定这可能很简单......但我做错了。

我正在尝试使用3个(现在的)项目创建解决方案:

  1. MCV Web App
  2. 模型
  3. DAL(实体框架)
  4. 我想将模型保存在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 - 数据库已创建,但没有表。

    无论如何,我确定我很紧张 - 任何帮助都会受到赞赏。

    由于

1 个答案:

答案 0 :(得分:6)

Clients需要成为实体框架的一个属性来提取它

public class InvoiceDataContext : DbContext
{
    public DbSet<Client> Clients { get; set; }
}
相关问题