I have a model created with Entity Framework (from my database). This has given me an auto-generated Partial Class with all the required Properties.
我有一个使用Entity Framework创建的模型(来自我的数据库)。这给了我一个自动生成的Partial Class,其中包含所有必需的属性。
I want to add a UIHint data annotation to one of these properties, but I don't want to add that code into the auto-generated file.
我想在其中一个属性中添加UIHint数据注释,但我不想将该代码添加到自动生成的文件中。
Is there a way of adding a UIHint to an existing property via another Partial Class? Or am I missing a fundamental part of how to use MVC?
有没有办法通过另一个Partial Class将UIHint添加到现有属性?或者我错过了如何使用MVC的基本部分?
UPDATE
juhan_h's answer worked perfectly, but here it is in VB.NET in case it helps anyone:
juhan_h的答案非常有效,但是在VB.NET中它可以帮助任何人:
Generated class
生成的类
Partial Public Class MyEntity
Public Property Id As Integer
Public Property Name As String
End Class
Metadata class
元数据类
<MetadataType(GetType(MyEntity_Metadata))>
Partial Public Class MyEntity
End Class
Class MyEntity_Metadata
<DisplayName("Key of the entity")>
Public Property Id As Integer
<UIHint("MyTextBox")>
Public Property Name As String
End Class
1 个解决方案
#1
1
You can use associated metadata classes to decorate your automatically generated classes with additional attributes.
您可以使用关联的元数据类来使用其他属性来装饰自动生成的类。
Here is an article describing the concept that should be applicable to your situation as well.
这篇文章描述了应该适用于您的情况的概念。
Example
例
Generated class
生成的类
public partial class MyEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
Metadata class
元数据类
[MetadataType(typeof(MyEntityMetadata))]
public partial class MyEntity
{
}
class Product_Metadata
{
[DisplayName("Key of the entity")]
public int Id { get; set; }
[UIHint("MyTextBox")]
public string Name { get; set; }
}
I do not really speak VB.NET so the examples are in c# but they should be easy to convert to VB.
我真的不会说VB.NET,所以这些例子都在c#中,但它们应该很容易转换为VB。
#1
1
You can use associated metadata classes to decorate your automatically generated classes with additional attributes.
您可以使用关联的元数据类来使用其他属性来装饰自动生成的类。
Here is an article describing the concept that should be applicable to your situation as well.
这篇文章描述了应该适用于您的情况的概念。
Example
例
Generated class
生成的类
public partial class MyEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
Metadata class
元数据类
[MetadataType(typeof(MyEntityMetadata))]
public partial class MyEntity
{
}
class Product_Metadata
{
[DisplayName("Key of the entity")]
public int Id { get; set; }
[UIHint("MyTextBox")]
public string Name { get; set; }
}
I do not really speak VB.NET so the examples are in c# but they should be easy to convert to VB.
我真的不会说VB.NET,所以这些例子都在c#中,但它们应该很容易转换为VB。