使用Entity Framework 5.0进行数据注释(数据库优先)
如果我使用entity framework(v5.0)数据库第一种方法,那么使用数据注释进行validation的最佳方法是什么?
这是我的Entity Framework创建的部分类:
//------------------------------------------------------------------------------ // // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //------------------------------------------------------------------------------ using System.ComponentModel.DataAnnotations; namespace ACore { using System; using System.Collections.Generic; public partial class PayrollMarkup_State { [UIHint("StatesEditor")] // <-- I added this line but it will be overwritten public string State { get; set; } public Nullable MaintenancePercentage { get; set; } public Nullable OfficePercentage { get; set; } } }
我试过这个没有成功……
entity framework生成的文件:’PayrollMarkup_State.cs’
//------------------------------------------------------------------------------ // // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //------------------------------------------------------------------------------ using System.ComponentModel.DataAnnotations; namespace ACore { using System; using System.Collections.Generic; public partial class PayrollMarkup_State { public string State { get; set; } public Nullable MaintenancePercentage { get; set; } public Nullable OfficePercentage { get; set; } } }
然后我在另一个目录中创建了这个文件:’PayrollMarkup_state.cs’
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace ACore.Models { [MetadataType(typeof(PayrollMarkupMetadata))] public partial class PayrollMarkup_State { } public class PayrollMarkupMetadata { [UIHint("StatesEditor")] public string State; // Has to have the same type and name as your model } }
虽然有点痛苦,但您需要创建一个类作为模型类的MetadataType
。
[MetadataType(typeof(PayrollMarkupMetadata)) public partial class PayrollMarkup_State { ... } public class PayrollMarkupMetadata { [UIHint("StatesEditor")] public string State; // Has to have the same type and name as your model // etc. }
您有一个名称空间问题 – 您已经定义了两个不同的PayrollMarkup_State类,一个在ACore名称空间下,另一个在ACore.Models名称空间下。 在包含元数据类型定义的文件中将名称空间更改为ACore(来自ACore.Models)。
您可以使用部分元数据类
https://www.asp.net/mvc/overview/getting-started/database-first-development/enhancing-data-validation
我使用了另外两个类:Map和Meta,这是我的地图:
namespace Whatever.Models { [MetadataType(typeof(ThisMeta))] public partial class This { } }
现在这里是元类:
上述就是C#学习教程:使用Entity Framework 5.0进行数据注释(数据库优先)分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Whatever.Models { public class ThisMeta { [DisplayName("")] public int UID { get; set; } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1003662.html