简体   繁体   English

使用Entity Framework手动将POCO映射到现有表

[英]Manually map POCO to existing Table using Entity Framework

I have an existing database table Movie . 我有一个现有的数据库表Movie Now I want to write a POCO Movie.cs which will map to that table. 现在我想编写一个映射到该表的POCO Movie.cs I want to have control over the mapping partially because I'm trying to learn more about EF. 我希望部分控制映射,因为我正在尝试了解有关EF的更多信息。

The Movie table has 3 fields: Title VARCHAR(255,), ReleaseDate DATETIME, Price MONEY . Movie表有3个字段: Title VARCHAR(255,), ReleaseDate DATETIME, Price MONEY What would my DbContext - derived class look like for this? 我的DbContext派生类会对此产生什么影响?

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. 来自Java / Hibernate背景,我想我只是将POCO中的字段映射到表中的cols,但它在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. “Code First” - >用C#编写所有内容,然后让EntityFramework生成并运行迁移,以不断更新和播种数据库。

  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 “数据库优先” - >创建数据库,然后在VS中创建一个数据库项目并生成一个EDMX文件,为您生成POCO

Both seem overly obtuse for the goal of mapping a single POCO to a single table. 对于将单个POCO映射到单个表的目标,两者似乎都过于迟钝。 What am I missing? 我错过了什么? I've heard reference to "Fluent API," but that seems to lead me back to 1. or 2. 我听说过“Fluent API”,但这似乎让我回到了1或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). 您可以编写POCO类,映射类并禁用迁移(迁移还涉及一个名为__MigrationHistory的元数据表,您在数据库中没有该表)。

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 (ie MyContext class). 为了禁用迁移和模型检查,我通常在上下文类中设置它(即MyContext类)。

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. 一个有趣的功能是来自数据库的CodeFirst。 EF generates the POCO classes starting from database. EF从数据库开始生成POCO类。 Classes often need a refactoring but is still better than writing classes from scratch. 类通常需要重构,但仍然比从头编写类更好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM