I'm developing an asp.net core 1.0 (MVC 6) full framework 4.6.1 and I created a Class Library targeting framework 4.6.1 for Data Modeling using Telerik DataAccess core & fluent 2016.1.224.
I have some classes, one of them is:
namespace DataModel
{
public partial class Directory
{
private int _id;
public virtual int Id
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
private string _directoryId;
public virtual string DirectoryId
{
get
{
return this._directoryId;
}
set
{
this._directoryId = value;
}
}
private string _directoryFullName;
public virtual string DirectoryFullName
{
get
{
return this._directoryFullName;
}
set
{
this._directoryFullName = value;
}
}
I need to customize Dataannotations to set display name & validations for every property. According telerik documentation, I'm declaring another partial class for original generated fluent Directory class to decorate it with MetadataType, like following:
[MetadataType(typeof(Directory.DirectoryMetadata))]
public partial class Directory
{
internal sealed class DirectoryMetadata
{
public DirectoryMetadata()
{
}
[Display(Name = "Id Directorio")]
[Required()]
public string DirectoryId
{ get; set; }
[Display(Name = "Nombre Completo")]
[Required()]
public string DirectoryFullName
{ get; set; }
When I run asp.net core app, MetadataType is not working, no display name nor validator works. However, for some reason, decorating original generated fluent class works fine!:
public partial class Directory
{
private int _id;
public virtual int Id
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
private string _directoryId;
[Display(Name = "Id Directorio")]
[Required()]
public virtual string DirectoryId
{
get
{
return this._directoryId;
}
set
{
this._directoryId = value;
}
}
private string _directoryFullName;
[Display(Name = "Nombre Completo")]
[Required()]
public virtual string DirectoryFullName
{
get
{
return this._directoryFullName;
}
set
{
this._directoryFullName = value;
}
}
Both classes, Original and metadata are in the same namespace. In fact, declaring Directory metadata class into a separate file instead internal doesn't work also.
Please, need your help!
答案 0 :(得分:1)
我自己找到了解决方案。
对于Asp.Net核心项目,我必须使用 ModelMetadataTypeAttribute 而不是 Microsoft.AspNetCore.Mvc.Core 程序集中的 MetadataTypeAttribute 。装饰元数据类(伙伴类)遵循使用Display(...),DisplayName(...)的相同方法,对于验证器也是如此。无论伙伴类(元数据类)是位于模型类的外部还是内部。
然而,将MetadataTypeAttribute直接与模型类而不是伙伴类一起使用,效果非常好!
我能解释的唯一解释是新出现的Microsoft Asp.Net核心技术,DLL过程和功能的重新定位。