简体   繁体   English

使用Entity Framework 5.0进行数据注释(数据库优先)

[英]Data Annotations with Entity Framework 5.0 (database first)

What is the best way to use data annotations for validation if I'm using an Entity Framework (v5.0) database first approach? 如果我使用Entity Framework(v5.0)数据库第一种方法,那么使用数据注释进行验证的最佳方法是什么?

This is my partial class created by Entity Framework: 这是我的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.... 我试过这个没有成功......

Entity Framework generated file: 'PayrollMarkup_State.cs' 实体框架生成的文件:'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; }
    }
}

I then created this file in a different directory: 'PayrollMarkup_state.cs' 然后我在另一个目录中创建了这个文件:'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
    }
}

Although it's somewhat painful, you need to create a class to use as the MetadataType for your model class. 虽然这有点痛苦,但您需要创建一个类作为模型类的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.
}

You have a namespace problem - you have defined two different PayrollMarkup_State classes, one under the ACore namespace and one under the ACore.Models namespace. 您有一个名称空间问题 - 您已经定义了两个不同的PayrollMarkup_State类,一个在ACore名称空间下,另一个在ACore.Models名称空间下。 Change the namespace to ACore (from ACore.Models) in the file containing the metadata type definition. 在包含元数据类型定义的文件中将名称空间更改为ACore(来自ACore.Models)。

I used two additional classes: Map and Meta, here is my map: 我使用了另外两个类:Map和Meta,这是我的地图:

namespace Whatever.Models
{
    [MetadataType(typeof(ThisMeta))]
    public partial class This
    {
    }


}

now here is meta class: 现在这里是元类:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Whatever.Models
{
    public class ThisMeta
    {

        [DisplayName("")]
        public int UID { get; set; }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM