使用代码首先从数据库自定义名称映射 [英] Custom name mapping using Code First From Database

查看:313
本文介绍了使用代码首先从数据库自定义名称映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑从LLBLGEN ORM转移到实体框架。我想用一个自定义命名约定映射数据库列名。

I am considering moving from LLBLGen ORM to Entity Framework. I would like to use a custom naming convention for mapping the DB column names.

我的表名与词之间下划线全部大写。即ACCOUNT_BALANCE

My table names are all capitals with underscores between words. i.e. ACCOUNT_BALANCE

所有的列名与词之间下划线资本:即FULL_NAME

All column names are capitals with underscores between words: i.e. FULL_NAME

我的主键都前缀与PK_或PFK_:即PK_ACCOUNT_BALANCE

My primary keys are all prefixed with PK_ or PFK_: i.e. PK_ACCOUNT_BALANCE

我的外键都前缀FK_:即FK_ACCOUNT

My foreign keys are all prefixed with FK_: i.e. FK_ACCOUNT

我有一个庞大的数据库建立,所以我想'从数据库代码第一'的方针使用具有自动映射器把ACCOUNT_BALANCE成AccountBalance,FULL_NAME到全名,PK_ACCOUNT_BALANCE到PkAccountBalance等。

I have a huge database to set up, so I would like to use the 'Code First from Database' approach with an auto-mapper to turn ACCOUNT_BALANCE into AccountBalance, FULL_NAME to FullName, PK_ACCOUNT_BALANCE to PkAccountBalance, etc.

我不介意让我的手脏,并进入了EF来源,但我目前正在努力找到最好的办法的任何指导。

I don't mind getting my hands dirty and going into the EF source, but I am currently struggling to find any guidance on the best approach.

任何帮助深表感谢。

解决方案

minhcat_vo的解决方案,下面有我开始。从那以后,我在VS解决方案右键点击,点击'实体框架 - >自定义逆向工程模板自定义模板添加到项目中。

minhcat_vo's solution below got me started. After that I right-clicked on the solution in VS, clicked 'Entity Framework -> Customise Reverse Engineer Templates' to add custom templates to the project.

然后,我创建[程序文件] \VisualStudio\Common7\IDE\Extensions\Microsoft\Entity框架Tools\Templates\的副本Includes\EF.Utility.CS.ttinclude(在同一个文件夹中)

Then I create a duplicate of [Program Files]\VisualStudio\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude (in the same folder)

然后在Context.tt,Entity.tt和Mapping.tt文件,我取代了参考对包括与新的(小于#@ include文件=EF.Utility.CS-MMS.ttinclude#><#@)

Then in the Context.tt, Entity.tt and Mapping.tt files I replaced the reference to the include with the new one (<#@ include file="EF.Utility.CS-MMS.ttinclude" #><#@)

之后我通过Context.tt,Mapping.tt和Entity.tt文件包裹与code.ContextName(...),code.EntityName(...)和code.PropertyName(上下文,实体和属性引用了...... )

After that I went through the Context.tt, Mapping.tt and Entity.tt files wrapping references to context, entities and properties with code.ContextName(...), code.EntityName(...) and code.PropertyName(...)

然后在新EF.Utility.CS.ttinclude文件我添加这些方法和PascalCase方法:

Then in the new EF.Utility.CS.ttinclude file I added those methods and a PascalCase method:

public string PascalCase(string name)
{
    return name == null || name.Length == 0
        ? ""
        : name.Length == 1
            ? name.ToUpperInvariant()
            : String.Join("",
                name.Split('_')
                    .Select(part => Char.ToUpperInvariant(part[0]) + part.Substring(1).ToLowerInvariant() )

                );
}

public string ContextName(string name)
{
    return PascalCase( name.Substring(0,name.Length - "Context".Length) ) + "Context";
}

public string EntityName(string name)
{
    return PascalCase(name);
}

public string PropertyName(string name)
{
    return PascalCase(name);
}



然后我右键单击的解决方案点击'逆向工程代码第一次来生成的类。

Then I right-clicked the solution a clicked 'Reverse Engineer Code First' to generate the classes.

我很想知道这是否是最好的办法。这似乎非常稳固,但。

I'd be interested to know whether this is the best approach. It seems pretty solid though.

推荐答案

我觉得EF晚于版本EF 4.3,它有一个特点 DbMigration ,以处理与开发商之间改变数据库的遗留系统。也许它可以帮助你以某种方式。

I think EF is later than version EF 4.3, it has a feature DbMigration to deal with the legacy system for changing db between developers. Maybe it could help you in some way.

如果您已经有现有的数据库,你还是第一次使用的代码。您可以下载 EF电动工具为Visual Studio扩展。它基本上生成的关系,一个实体的实体模型 - 一个表,所有的地图文件。

If you already have your existing db, you still use code first. You can download EF Power Tool extension for Visual Studio. It basically generate your entity model with the relation one entity - one table, and all map files.

这篇关于使用代码首先从数据库自定义名称映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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