使用实体框架将POCO手动映射到现有的表 [英] Manually map POCO to existing Table using Entity Framework

查看:124
本文介绍了使用实体框架将POCO手动映射到现有的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据库表 Movie 。现在我想写一个映射到该表的POCO Movie.cs 。我想控制映射,部分是因为我正在尝试更多地了解EF。



电影表格有3个字段:标题VARCHAR(255,),ReleaseDate DATETIME,Price MONEY 。我的DbContext派生类是什么样的?



从Java / Hibernate的背景出发,我想我将POCO中的字段映射到表中的cols ,但在EntityFramework中看起来并不直接。



我似乎只找到这两个选项:


  1. Code First - >将所有内容写入C#,然后让EntityFramework生成和运行迁移,以不断更新和种子数据库。


  2. 数据库优先 - >创建数据库,然后在VS中创建一个DB项目,并生成一个为您生成POCO的EDMX文件


对于将单个POCO映射到单个表的目标,两者似乎都过于钝。我失踪了什么我听说过流利的API,但这似乎让我回到了1.或者2。



我努力找到文档中一致的内容我正在寻找。

解决方案

你有另一个选择。您可以编写POCO类,映射类并禁用迁移(迁移还涉及您的数据库中没有的名为__MigrationHistory的元数据表)。



该类可以(使用属性映射字段,您可以使用流畅的界面)

  class Movie {
[PrimaryKey ]
[MaxLength(255)
public string Title {get; set;}
public datetime ReleaseDate {get; set;}
public float Price {get; set;} //或者你推荐的类型
}

要禁用迁移和模型检查我通常将它设置在上下文类(即MyContext类)中。

  class MyContext:DbContext {

static MyContext()
{
System.Data.Entity.Database.SetInitializer< CloseUpContext>(null); //这将禁用模型检查
}

public DbSet< Movie>电影{get; set;}


}

一个有趣的功能是CodeFirst从数据库。 EF从数据库开始生成POCO类。课程通常需要重构,但仍然比从头开始写课程还要好。


I have an existing database table Movie. Now I want to write a POCO Movie.cs which will map to that table. I want to have control over the mapping partially because I'm trying to learn more about EF.

The Movie table has 3 fields: Title VARCHAR(255,), ReleaseDate DATETIME, Price MONEY. What would my DbContext - derived class look like for this?

Coming from a Java/Hibernate background, I figured I would just map fields in the POCO to cols in the table, but it doesn't seem that straight forward in EntityFramework.

I only seem to find these 2 options:

  1. "Code First" -> write everything in C#, then let EntityFramework generate and run migrations to constantly update and seed the DB.

  2. "Database First" -> create the DB, then create a DB project in VS and generate an EDMX file which generates the POCO's for you

Both seem overly obtuse for the goal of mapping a single POCO to a single table. What am I missing? I've heard reference to "Fluent API," but that seems to lead me back to 1. or 2.

I struggled to find anything consistent in the docs that described what I was looking for.

解决方案

You have another option. You can write POCO class, map the class and disable migrations (migration involves also a metadata table called __MigrationHistory that you don't have in your database).

The class could be (using attributes to map the fields, you can use fluent interface as well)

class Movie {
    [PrimaryKey]
    [MaxLength(255)
    public string Title {get; set;}
    public datetime ReleaseDate {get; set;}
    public float Price {get; set;} // Or the type you prefere
}

For disable the migration and the model checking I usually set it in the context class (i.e. MyContext class).

class MyContext : DbContext {

    static MyContext()
    {
        System.Data.Entity.Database.SetInitializer<CloseUpContext>(null); // This disables model checking
    }

    public DbSet<Movie> Movies {get; set;}


}

An interesting feature is CodeFirst from Database. EF generates the POCO classes starting from database. Classes often need a refactoring but is still better than writing classes from scratch.

这篇关于使用实体框架将POCO手动映射到现有的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆