覆盖实体框架实体属性 [英] Override Entity Framework Entity Property

查看:168
本文介绍了覆盖实体框架实体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在EF名为档案的实体,我想数据注解属性添加到该实体的名字属性,因此我创造了像这样一个新的部分类;

I have an entity in EF named Profile and I would like to add data annotation attributes to the "FirstName" property of this entity so I created a new partial class like so;

public partial class Profile : EntityObject
{
    [Required]
    [Display(Name = "First Name")]
    [EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
    [DataMemberAttribute()]
    override public global::System.String FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            OnFirstNameChanging(value);
            ReportPropertyChanging("FirstName");
            _FirstName = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("FirstName");
            OnFirstNameChanged();
        }
    }
}

但我得到这个;

But I am getting this;

The type 'CC.Models.Profile' already contains a definition for 'FirstName'

任何想法?

问候,

瑞安

推荐答案

您可惜不能像改变它。你必须创建元数据类,并添加元数据属性的类。见下面的链接:

You unfortunately can't change it like that. You have to create a metadata class and add the metadata attributes to that class. See below link:

<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx\">http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

看一看的链接,看到生成的元数据类的一些问题,我通常做的是,如果我改变的东西我只是通过添加新服务,之后删除该服务,然后合并这两个让我的老的变化,并保持新添加的实体再生metadataclass。

Have a look at this link to see some issues with generating a metadata class, what I normally do is if I change something I just regenerate the metadataclass by adding a new service and deleting the service afterwards and then merge the two keeping my old changes and keeping the newly added entities.

这篇关于覆盖实体框架实体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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