简体   繁体   English

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

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

Evening, Im sure that this is probably quite simple... but Im doing something wrong. 晚上,我相信这可能很简单......但我做错了。

I am trying to create a solution with 3 (for now) projects: 我正在尝试使用3个(现在的)项目创建一个解决方案:

  1. MCV Web App MCV Web App
  2. Models 楷模
  3. DAL (Entity Framework) DAL(实体框架)

I want to keep the models in a different project/assembly from the UI and the DAL because the models will be reusable between projects and we may want to swap out the DAL etc without having to mess about with the models etc... 我想将模型保存在UI和DAL的不同项目/程序集中,因为模型可以在项目之间重用,我们可能想要更换DAL等而不必弄乱模型等...

Anyway, onto the problem. 无论如何,解决问题。

I have created a few classes in my models project like : 我在我的模型项目中创建了一些类,如:

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; }
}

I have then created a DBContext in the DAL 然后我在DAL中创建了一个DBContext

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

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

I have enabled migrations on the DAL project, Added a migration and called update-database - the database has been created but with NO tables. 我已经在DAL项目上启用了迁移,添加了一个迁移并调用了update-database - 数据库已经创建但没有表。

Anyway, Im sure Im being dense - any help will be appreciated. 无论如何,我确定我是密集的 - 任何帮助将不胜感激。

Thanks 谢谢

Clients needs to be a property for Entity Framework to pick it up Clients需要成为实体框架的一个属性才能获取它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先-更新不同项目中的数据库 - Entity Framework code first - update database in different project 重新生成实体框架代码优先模型 - Regenerate Entity Framework Code First Models 实体框架代码第一个数据库未与模型同步 - Entity Framework code first database not synced with models 在实体框架中扩展和自定义代码优先模型 - Extending and Customizing Code First Models in Entity Framework 是否可以将数据库优先模型和代码优先模型与实体框架混合使用? - Is it possible to mix database first and code first models with entity framework? 实体框架代码首先在数据库中重命名模型/表 - Entity Framework Code first rename models/tables in Database 如何使用Entity Framework 7在代码中首先处理这两个模型? - How handle these two models in code first approach using Entity Framework 7? Entity Framework Code First - 在域模型上使用接口 - Entity Framework Code First - Using interfaces on domain models 如何在同一项目中使用 2 个不同的数据库架构组织 Entity Framework Core 迁移(代码优先)? - How to organize Entity Framework Core migrations (code first) with 2 different databases schema in the same project? 实体框架代码优先:在具有不同连接列的实体中展平组合 - Entity Framework Code First : flatten composition in entity with different join column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM