简体   繁体   English

如何使用Entity Framework将POCO模型类映射到现有数据库表

[英]How do I map POCO model classes to existing DB tables using Entity Framework

We have existing POCO model classes and an existing DB. 我们有现有的POCO模型类和现有的DB。 The OO model classes and relational DB tables don't map directly to each other. OO模型类和关系数据库表不直接相互映射。

How do I use Entity Framework to map these? 如何使用Entity Framework映射这些? All of the tutorials I have seen are either Code First (which goes and generates new DB tables), Model first (which generates new DB tables and POCOs) and DB First (which generates new POCOs). 我见过的所有教程都是Code First(生成新的DB表),Model First(生成新的DB表和POCO)和DB First(生成新的POCO)。

I want none of these! 我不想要这些! I want to map between existing POCOs and DB tables. 我想在现有的POCO和数据库表之间进行映射。 What's the best way of doing this? 这样做的最佳方法是什么? Can I use the visual designer, or do I have to do it in code? 我可以使用可视化设计器,还是必须在代码中使用它?

Create mappings which map your entities to database structure (I suggest to use fluent mappings). 创建将实体映射到数据库结构的映射(我建议使用流畅的映射)。 Then just disable database generation: 然后只禁用数据库生成:

Database.SetInitializer<YourContext>(null);

and provide connection string to existing database: 并为现有数据库提供连接字符串:

public class YourContext : DbContext
{
    public DatabaseContext(string connectionStringName)
      : base(connectionStringName)
    {
    }
}

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

相关问题 使用Entity Framework手动将POCO映射到现有表 - Manually map POCO to existing Table using Entity Framework 我是否需要将POCO类与Entity Framework 6一起使用 - Do I need to use POCO Classes with Entity Framework 6 当我拥有现有的POCO类和具有复杂/自定义映射的表时,如何使用EF4? - How can I use EF4 when I have existing POCO classes and existing tables with complex/custom map pings? 仅使用某些类的带有POCO的实体框架6 - Entity Framework 6 with POCO Using Only Some Classes 如何在实体框架的POCO中为这种关系建模? - How would I model this relation in my POCO's in Entity Framework? 如何使用Entity Framework Code First CTP5按年份查询相同的数据库表? - How do I query identical DB tables by year using Entity Framework Code First CTP5? 使用代码优先实体框架的数据访问层、模型层和 POCO 类 - DataAccess layer, Model layer and POCO classes with Code First Entity Framework 从Entity Framework 6 Model中分离POCO Object类和DBContext - Separate POCO Object classes and DBContext from Entity Framework 6 Model 实体框架:将现有子POCO添加到新的父POCO,在DB中创建新子项 - Entity Framework: adding existing child POCO to new Parent POCO, creates new child in DB 使用Entity Framework将模型映射到现有数据库表 - Map model to existing database table using Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM