数据注释与实体框架5.0(数据库第一) [英] Data Annotations with Entity Framework 5.0 (database first)
本文介绍了数据注释与实体框架5.0(数据库第一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我使用实体框架(v5.0)数据库的第一种方式,使用数据注释的最佳方式是什么?
What is the best way to use data annotations for validation if I'm using an Entity Framework (v5.0) database first approach?
这是我的部分实体框架创建的类:
This is my partial class created by Entity Framework:
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
//------------------------------------------------------------------------------
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<float> MaintenancePercentage { get; set; }
public Nullable<float> OfficePercentage { get; set; }
}
}
我没有成功尝试过....
I tried this with no success....
实体框架生成的文件:'PayrollMarkup_State.cs'
Entity Framework generated file: 'PayrollMarkup_State.cs'
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
//------------------------------------------------------------------------------
using System.ComponentModel.DataAnnotations;
namespace ACore
{
using System;
using System.Collections.Generic;
public partial class PayrollMarkup_State
{
public string State { get; set; }
public Nullable<float> MaintenancePercentage { get; set; }
public Nullable<float> OfficePercentage { get; set; }
}
}
然后我在另一个目录中创建了这个文件: 'payrollMarkup_state.cs'
I then created this file in a different directory: '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
。
Although it's somewhat painful, you need to create a class to use as the MetadataType
for your model class.
[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.
}
这篇关于数据注释与实体框架5.0(数据库第一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文