3

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; }

    }
}

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.

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

4

1 回答 1

6

Code First 并没有正式支持逆向工程场景,这就是它被称为 Code First 的原因。电动工具很好,但正如你所说,它们会覆盖东西。EF Code First 旨在让您对模型本身进行更改,而不是对部分和元数据进行更改(尽管您仍然可以根据需要使用它们)。

您可以使用 EF Power Tools 的自定义逆向工程师模板功能,然后修改 T4 模板以将部分声明添加到生成的类,或者您可以添加代码来测试特定实体并根据需要进行修改。

但是,实际上,您不需要这样做。将实体传递给视图被认为是糟糕的设计,这是将这些属性添加到模型的唯一原因。最重要的是,您强制视图具有与数据模型相同的要求,这也是一个糟糕的选择。

相反,您应该在视图中使用专用视图模型,并将数据属性放在视图模型上。

于 2012-10-10T19:34:26.763 回答