如果EF 5定义一个自定义命名约定 [英] How to define a custom naming convention if EF 5

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

问题描述

假设我有一个名为 Products 的表的数据库。所以,我经历了Db First方法,并在VS 2012中创建了一个EF模型,其中 pluralize和singularize 选项。

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.

现在我想改变这个行为。实际上,我想创建一个自定义约定,将 ProductModel 实体映射到 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.

所以我决定创建一个 ProductModel 类并将产品代码复制到其中,并将视图作为视图模型传递。然后,当我想查询我的数据库时,我会收到一个异常,表示db中不存在 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...

提前感谢

推荐答案

默认情况下,数据库优先方法将始终映射一个同名的实体的表名(可能是多个/单数)。 EF目前没有规定通过惯例或工具进行调整。您可以更改/调整/自定义您的模型 - 但如果您重新生成,那些更改将丢失。我不知道任何工具/脚本/黑客以某种方式保留这些更改并在从数据库重新生成模型后重新应用它们。

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(..)属性如此SO问题及其答案所示

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 ,如下所示:

and then define the metadata/data annotations for your products class in yet another file, ProductMetadata.cs, like this:

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

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

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