使用分部类添加数据注释 [英] Add data annotations using partial class
问题描述
嗨!
Hi!
我正在使用DbFirst,我尝试将数据注释添加到现有生成的代码中。
I'm using DbFirst and I trying to add data annotation to existing generated code.
里面有edmx已经这个班级
inside edmx there is already this class
namespace POSweb.Models
{
public partial class GS_POS_CO
{
.....
public long ID { get; set; }
.....
}
}
为了避免在模型刷新时覆盖代码,我创建了一个新文件Annotations.cs,并在里面为类和名称空间添加一个同名的新的分部类:
To avoid code overwritten on model refresh I've created a new file Annotations.cs and inside add a new partial class with the same name for class and namespace:
namespace POSweb.Models
{
[MetadataType(typeof(GS_POS_CO.Annotations))]
public partial class GS_POS_CO
{
internal class Annotations
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long ID { get; set; }
}
}
}
没有错误,但我的代码似乎不起作用,必须添加我必须放置的新类文件?
There is no error but my code doesn't seems to work,there must be the new class file added that I have to put is somewhere ?
谢谢
推荐答案
你好mrapi,
Hi mrapi,
根据你的说法描述和相关代码,似乎你在两个部分类中有相同的属性名为ID,这导致问题,请删除一个。
According to your description and related code, it seems that you have the same property named ID in two partial class, which cause the issue, please remove one.
我们知道,每个源文件都包含一个部分类型或方法定义,并在编译应用程序时组合所有部分。
As we know, Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.
有关部分类的更多信息,请参阅:
For more information about partial class, please refer to:
祝你好运,
Cole
这篇关于使用分部类添加数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!