简体   繁体   English

带有实体框架6的手工POCO?

[英]Handmade POCO with Entity Framework 6?

I'm looking into using EF 6 for an upcoming small project I will be doing at a very large organization. 我正在考虑将EF 6用于即将在一个大型组织中进行的小型项目。 Having POCO business objects is essential to me, but all of the options I can find for how to do POCO with EF all seem to rely on auto-generating tools that try to map an entire database structure to POCO objects. 拥有POCO业务对象对我来说是必不可少的,但是我找到的有关使用EF进行POCO的所有选择似乎都依赖于自动生成工具,这些工具试图将整个数据库结构映射到POCO对象。 We have many databases, hundreds of tables, and hundreds of views, and I'm only looking to use a few of these tables for the time being (and any one project would only hope to use a small subset of them). 我们有许多数据库,数百个表和数百个视图,并且我暂时只想使用其中一些表(并且任何一个项目都只希望使用其中的一小部分)。

Additionally I don't want to necessarily map out the many foreign properties or even some of the normal properties that these tables store. 另外,我不想映射出这些表存储的许多外部属性,甚至某些普通属性。 So I'd really like to make these POCO objects by hand and then connect them to EF and let it do the mapping - I'd like to still be able to use an EDMX file and not have to make my own ObjectContext, if possible. 所以我真的很想手工制作这些POCO对象,然后将它们连接到EF并让它进行映射-我希望仍然能够使用EDMX文件,并且如果可能的话不必创建自己的ObjectContext 。

I feel as though this has to be dead simple, but I can't really find any resources on it! 我觉得这似乎很简单,但是我真的找不到任何资源! If anyone can point me in the right direction that would really be helpful. 如果有人能指出正确的方向,那将真的很有帮助。

Entity Framework has a Code First aspect that can just map to existing tables as well. 实体框架具有“代码优先”方面,也可以仅映射到现有表。 This allows you to use POCOs to represent your tables. 这使您可以使用POCO来代表您的表。 You can create Mapping Classes that allow you to separate your POCOs from your database mapping logic. 您可以创建映射类,使您可以将POCO与数据库映射逻辑分开。

You can specify if there is a different naming of the table column from the POCO. 您可以指定表列的命名与POCO是否不同。 If there is no difference between the two however, you can omit it. 但是,如果两者之间没有区别,则可以忽略它。

using ProjectName.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class ProfileMapping : EntityTypeConfiguration<Profile>
{
    public ProfileMapping()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Map POCO name to Column Name
        this.Property(t => t.Firstname)
            .HasColumnName("First_Name")
            .HasMaxLength("256");


        // Table Mapping
        this.ToTable("Profiles");

        // Relationships
        this.HasRequired(t => t.Roles);
    }
}

Using the DbContext you can register these mappings. 使用DbContext可以注册这些映射。

I would recommend using the Entity Framework Power Tools to generate your DbContext from an existing database. 我建议使用Entity Framework Power Tools从现有数据库生成DbContext。

http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

Check out http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/setting-up-database 查看http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/setting-up-database

Create a edmx for each database and you can select which objects you want to import. 为每个数据库创建一个edmx,然后可以选择要导入的对象。

If you have an existing database look at database first. 如果您已有数据库,请首先查看数据库。

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

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