【问题标题】:Adding attributes to Entity Framework autogenerated properties将属性添加到实体框架自动生成的属性
【发布时间】:2013-08-07 08:18:03
【问题描述】:

我需要为实体框架自动生成的属性添加一些属性,并且不想在重新生成对象时丢失它们。我也不想碰T4。

查看interenet我发现部分类可以添加MetaDataType,如:

<MetadataType(GetType(Employee_Metadata))> _
Partial Public Class employee
...

然后创建另一个类,我们将实际的元数据添加到属性中:

Public Class Employee_Metadata

    <Category("General"), DisplayName("Name"), Description("Employee name.")> _
    Public Property employee_name() As String
        Get
            Return _employee_name
        End Get
        Set(value As String)
            _employee_name = value
        End Set
    End Property
    Private _employee_name As String

End Class

现在,我还需要做什么才能访问这些属性? 我目前正在将 UI 组件绑定到类员工自动生成的属性“employee_name”(使用 MVVM)。我是否需要在部分类中进一步更改某些内容,还是应该更改数据绑定本身(在本例中为 WPF)?

【问题讨论】:

    标签: entity-framework properties attributes metadata


    【解决方案1】:

    这应该可行:

    1. 为设计器中的映射属性选择不同的名称(例如 employee_name_private)并将设计器中的 setter 和 getter 标记为 Private
    2. 在部分类文件中定义具有所需属性的 public 属性:

      Partial Public Class Employee
      
          <Category("General"), DisplayName("Name"), Description("Employee name.")> _
          Public Property employee_name() As String
              Get
                  Return employee_name_private
              End Get
              Set(value As String)
                  employee_name_private = value
              End Set
          End Property
      
      End Class
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-27
      • 2011-04-27
      • 2010-11-06
      • 2011-05-26
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      相关资源
      最近更新 更多