是否有可能prevent的EntityFramework 4覆写自定义的属性? [英] Is it possible to prevent EntityFramework 4 from overwriting customized properties?

查看:144
本文介绍了是否有可能prevent的EntityFramework 4覆写自定义的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF 4数据库第一+波苏斯。因为EF没有简单的方法来声明传入DateTime是否是一种UTC的,我在另一个文件移动从自动生成的文件的属性设置为部分类。

I am using EF 4 Database first + POCOs. Because EF has no easy way to state that incoming DateTimes are of kind UTC, I moved the property from the auto-generated file to a partial class in another file.

    private DateTime _createdOn;
    public virtual System.DateTime CreatedOn
    {
        get { return _createdOn; }
        set
        {
            _createdOn =
                (value.Kind == DateTimeKind.Unspecified)
                    ? _createdOn = DateTime.SpecifyKind(value, DateTimeKind.Utc)
                    : value;
        }
    }

不过,现在我每次更新模型,自动化性能再次获得了T4代创建。当然,这将导致以下编译错误:类型'富'已经包含了'CreatedOn'的定义

However, now every time I update the model, the automated properties get created again in the T4-generation. Of course this causes the following compilation error: "The type 'Foo' already contains a definition for 'CreatedOn'".

有没有办法告诉EF不产生财产,并让我处理这件事对我自己的?

Is there any way to tell EF to not generate that property and to let me handle it on my own?

更新

感谢大家的答案......

Thanks for everyone's answers...

我创建了一个新的自定义属性使用不同的名称。

I created a new custom property with a different name.

    public virtual System.DateTime CreatedOnUtc
    {
        get
        {
            return (CreatedOn.Kind==DateTimeKind.Unspecified)
                ? DateTime.SpecifyKind(CreatedOn, DateTimeKind.Utc)
                : CreatedOn;
        }
        set
        {
            CreatedOn =
                (value.Kind == DateTimeKind.Unspecified)
                    ? CreatedOn = DateTime.SpecifyKind(value, DateTimeKind.Utc)
                    : value;
        }
    }

我还设置所有的setter和自动生成的属性干将给私人使用,我需要在LINQ到实体查询(叹气)使用这些属性的除外。在这种情况下,我设置这些干将内部。

I also set all of the setters and getters of the auto-generated property to Private with the exception of those properties that I needed to use in a Linq-to-Entities query (sigh). In those cases, I set those getters to internal.

我当然希望有日期时间类型下拉指定什么好心肠的日期时间的EF应该把它看作。这将有可能挽救小时,额外的复杂性。

I sure wish there was a dropdown on DateTime types to specify what "Kind" of DateTime that EF should treat it as. That would have saved hours and the extra complication.

推荐答案

我觉得事情开始变得混乱,如果您尝试手动修改EF生成的类。

I think things start to get messy if you try to manually modify EF generated classes.

有两种选择,我会建议追求:

There are two options that I'd suggest pursuing:


  1. 请不要修改现有的属性,但添加一个新的你的部分类, CreatedOnUTC 或类似的东西。

  2. 修改T4模板来改变它生成这些日期属性的访问者(更容易,如果每一个日期时间属性是相同的方式工作)的方式。这不会是微不足道的,因为它的类型有关,但至少会允许你使用发电机的未来。

  1. Don't modify the existing property, but add a new one to your partial class, CreatedOnUTC or something similar.
  2. Modify the T4 template to alter the way that it generates the accessors for these date properties (easier if every DateTime property is to work the same way). It won't be trivial, since it's type dependent, but would at least allow you to use the generator in future.

这篇关于是否有可能prevent的EntityFramework 4覆写自定义的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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