实体框架 - 逆向工程code首先 - 改写变化 [英] Entity Framework - Reverse Engineer Code First - Overwriting Changes

查看:177
本文介绍了实体框架 - 逆向工程code首先 - 改写变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用的EF电动工具为逆向工程现有数据库。这可以。根据EDMX数据库-1路由我会像数据注释,对于任何例如创建覆盖部分类在这里, LedgerPeriod 是原始EntityObject:

Im using the EF Power Tools to Reverse Engineer an existing Database. This is fine. Under the EDMX Database-1st Route I would create partial classes for any overrides like Data Annotations, for e.g. here, LedgerPeriod being the original EntityObject:

[MetadataType(typeof(LedgerPeriodMetaData))]
public partial class LedgerPeriod
{
    public class LedgerPeriodMetaData
    {

        [Required(ErrorMessage = "Period Start Date Required")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [DataType(DataType.Date)]
        public object PeriodDateFrom { get; set; }

        [Required(ErrorMessage = "Period End Date Required")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [DataType(DataType.Date)]
        public object PeriodDateTo { get; set; }

    }
}

这是罚款。现在做我使用的 code首先逆向工程师同样的方法或是否有任何自定义设置,即如果你运行的电动工具多次,因为说我增加了一个新的表到数据库,就无法覆盖到现有车型的任何更改创建previously。

This is fine. Now do I use the same approach for Code First Reverse Engineer or are there any custom settings whereby if you have to run the power tools multiple times, because say I added a new Table to the Database, it wouldn't overwrite any changes to the existing Models created previously.

这将最终意味着我可以改变实际的模型类本身,而不是创造局部类。

This would ultimately mean I can make changes to the actual model classes themselves, instead of creating partial classes.

参考了EF电动工具可以在这里找到:

Reference to the EF Powertools can be found here:

http://blogs.msdn.com/b/adonet/archive/2012/04/09/ef-power-tools-beta-2-available.aspx

推荐答案

code首先不正式支持逆向工程的情况,这就是为什么它被称为code首先。电动工具是不错,但正如你所说,他们将覆盖东西。 EF code首先是设计,让你让你自己改变的模型,而不是泛音和元数据(虽然你仍然可以使用这些如果你想)。

Code First does not officially support the reverse engineering scenario, that's why it's called Code First. The power tools are nice, but as you say, they will overwrite things. EF Code First is designed so that you make your changes to the models themselves, not partials and metadata (though you can still use those if you want).

你可以做的是使用EF电动工具的自定义逆向工程模板功能,然后修改T4模板分部声明添加到生成的类,或者你可以添加code测试特定实体和修改这些只要你想。

What you could do is use the Customize Reverse Engineer Templates feature of EF Power Tools, then modify the T4 templates to add partial declarations to the generated classes, or you could add code to test for specific entities and modify those as you want.

然而,在现实中,你不应该需要做到这一点。它被认为不好的设计,你的实体传递到您的看法,这将是对那些属性添加到模型的唯一原因。最重要的是,你强迫你的看法有相同的要求,你的数据模型,这也是一个不错的选择。

However, in reality, you shouldn't need to do this. It's considered bad design to pass your entities to your Views, and that would be the only reason to add those attributes to the models. On top of that, you're forcing your views to have the same requirements as your data model, which is also a bad choice.

您应该改为正在使用专用视图模型与您的意见,并放置在您的视图模型的数据属性。

You should instead be using dedicated view models with your views, and place your data attributes on your view models.

这篇关于实体框架 - 逆向工程code首先 - 改写变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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