覆盖实体框架实体属性

发布于 2024-11-01 10:18:45 字数 879 浏览 0 评论 0原文

我在 EF 中有一个名为 Profile 的实体,我想向该实体的 FirstName 属性添加数据注释属性。所以,我创建了一个新的部分类,如下所示;

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

但我明白了;

类型“CC.Models.Profile”已包含“FirstName”的定义

有什么想法吗?

问候,
瑞安

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'

Any ideas?

Regards,
Ryan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未央 2024-11-08 10:18:45

不幸的是你不能这样改变它。您必须创建一个元数据类并将元数据属性添加到该类。请参阅以下链接:

http://msdn.microsoft.com/en-us/ Library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

看看 链接可查看生成元数据类的一些问题,我通常做的是如果我更改了一些内容,我只是重新生成通过添加新服务并随后删除该服务来创建元数据类,然后合并这两个服务,保留旧的更改并保留新添加的实体。

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:

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

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.

暖树树初阳… 2024-11-08 10:18:45

遗憾的是,您无法向 POCO 中生成的属性添加任何注释。

一种可能的解决方法是修改 TT 模板,为您只想定位的给定属性+实体添加所需的自定义注释。

我在之前的项目中,当我想使用Enterprise Library来实现验证时,也遇到了同样的问题。我最终创建了一个分部类并编写了用注释修饰的方法。

在您的情况下,您可以尝试在分部类中创建一个属性(具有不同的名称),该属性返回顶部带有注释的 EF 实体属性。

You cannot unfortunately add any annotations to the properties generated in the POCO.

A possible workaround is to modify the TT template to add the custom annotation that you want for the given property+entity that you want to target only.

I had the same issue in a previous project when I wanted to use the Enterprise Library to implement validation. I ended up creating a partial class and writing methods decorated with annotations instead.

In your case, you can try creating in a partial class a property (with a different name) that returns the EF entity property with an annotation on top.

紫南 2024-11-08 10:18:45

我相信您也必须将您的财产标记为部分财产。

实际上我认为您也想删除覆盖(因为您没有覆盖父属性。)

I believe you have to mark your property as partial as well.

Actually I think you also will want to remove the override (because you aren't overriding a parent property.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文