如何界定是否EF 5自定义命名约定 [英] How to define a custom naming convention if EF 5

查看:189
本文介绍了如何界定是否EF 5自定义命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为1台产品数据库。所以,我通过数据库优先的方法,并创建VS 2012的EF模型复数和单数选项。

Suppose that I have a database with 1 table named Products. So, I go through the Db First approach and create an EF model in VS 2012 with pluralize and singularize option.

因此​​,其型号为我创建了一个产品实体,并且默认的命名约定这个实体映射到 dbo.Products 表。

So, the model creates a Product entity for me, and default naming convention maps this entity to the dbo.Products table.

现在我想改变这种行为。其实我是想创建一个自定义的公约地图产品型号实体到 dbo.Products 表。

Now I want to change this behavior. In fact I want to create a custom convention to map ProductModel entity to the dbo.Products table.

这是可能的?如果是这样,怎么样?

Is this possible?! If so, how?

更新:我的目标做...

你知道,当你从更新数据库模型,如果会导致你的模型的变化,自动生成的实体将被覆盖。

As you know, whenever you update your model from database, if it causes a change in your model, the auto-generated entities will be over written.

从另一方面,我想补充的数据注解属性的实体属性,这样我可以用它们来塑造自己的看法,并希望简单地用我的工作一样的DbContext以下插入:

From the other hand, I want to add data annotation attributes to entity properties so that I can use them to shape my views and want to simply work with my DbContext like the following insert:

public ActionResult Create(Product product)
    {
        if (ModelState.IsValid)
        {
            db.Products.Add(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(product);
    }

问题是,我的应用程序的分析未完成和数据库的变化倍至倍。所以,我需要从数据库更新模型,并在此之后,我的所有属性都会删除。

the problem is that my application analysis isn't completed and the database changes times to times. So, I need to update the model from the database and after that, all my attributes will removed.

所以我决定创建一个产品型号类,并复制产品 codeS到它,并通它的意见,视图模型。然后,我whene要查询我的分贝,我会得到一个异常,说, dbo.ProductModels 名称未在数据库中存在...

So I decided to create a ProductModel class and copy the Product codes to it, and pass it the views as view model. Then, whene I want to query my db, I'll get an exception which says that the dbo.ProductModels name is not exist in the db...

在此先感谢

推荐答案

默认情况下,数据库优先的方法总是一个表名映射到同一名称的实体(可能是复数/ singularized)。目前在EF没有规定由公约或工具来调整的。您可以更改/调整/自定义模型 - 但是如果你重新生成,这些更改将丢失。我不知道任何工具/脚本/黑客以某种方式preserve这些变化,并从数据库中重新生成模型后,再应用它们。

By default, a database-first approach will always map a table name to an entity of the same name (possibly pluralized/singularized). There is currently no provision in EF to tweak that by conventions or tooling. You can change/adapt/customize your model - but if you regenerate it, those changes are lost. I'm not aware of any tools/scripts/hacks to somehow "preserve" those changes and re-apply them after regenerating the model from the database.

如果您需要扩展生成的我建议使用的事实,这些都是部分类 - 你可以的延长的在第二个物理文件生成的类:

If you need to extend the generated I'd suggest using the fact that those are partial classes - you can extend the generated class in a second physical file :

在一个单独的文件中写入此,例如 ProductExtension.cs

Write this in a separate file, e.g. ProductExtension.cs:

public partial class Product
{
   // add your custom methods etc. here
}

这弥补该类这些各种文件将被C#编译器为你一起合并到一个班

Those various files that make up that class will be merged together into one class by the C# compiler for you.

如果你需要数据的注释添加到现有生成的类,则可以使用MetadataType(..)属性<一个href=\"http://stackoverflow.com/questions/16736494/add-data-annotations-to-a-class-generated-by-entity-framework\">as在这太问题显示其答案的:

If you need to add data annotations to existing generated classes, you can use the MetadataType(..) attribute as shown in this SO question and its answers:

在一个单独的文件中写入此,例如 ProductExtension.cs

Write this in a separate file, e.g. ProductExtension.cs:

[MetadataType(typeof(ProductMetaData))]
public partial class Product
{
}

,然后定义元数据/数据​​注释为你的产品类另一个文件, ProductMetadata.cs ,就像这样:

public class ProductMetaData
{
    [Required]
    public int RequestId {get;set;}
    //...
}

这篇关于如何界定是否EF 5自定义命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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