与实体框架5.0数据注释(数据库第一) [英] Data Annotations with Entity Framework 5.0 (database first)

查看:102
本文介绍了与实体框架5.0数据注释(数据库第一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是如果我使用的是实体框架(V5.0)数据库第一种方法使用数据的注释进行验证的最好方法?

这是我通过实体框架创建部分类:

  // ------------------------------------ ------------------------------------------
//<自动生成>
//这个code是从模板生成。
//
//这个文件的手动更改可能会导致应用程序意外行为。
//如果code再生此文件的手动更改将被覆盖。
//< /自动生成>
// ------------------------------------------------ ------------------------------使用System.ComponentModel.DataAnnotations;命名空间ACORE
{
    使用系统;
    使用System.Collections.Generic;    公共部分类PayrollMarkup_State
    {
        [UIHint(StatesEditor)] //< - 我加了这条线,但它会被覆盖
        公共字符串状态{搞定;组; }
        公众可空<浮动> MaintenancePercentage {搞定;组; }
        公众可空<浮动> OfficePercentage {搞定;组; }
    }
}

我尝试这个没有成功......

实体框架生成的文件:PayrollMarkup_State.cs

  // ------------------------------------ ------------------------------------------
//<自动生成>
//这个code是从模板生成。
//
//这个文件的手动更改可能会导致应用程序意外行为。
//如果code再生此文件的手动更改将被覆盖。
//< /自动生成>
// ------------------------------------------------ ------------------------------使用System.ComponentModel.DataAnnotations;命名空间ACORE
{
    使用系统;
    使用System.Collections.Generic;    公共部分类PayrollMarkup_State
    {
        公共字符串状态{搞定;组; }
        公众可空<浮动> MaintenancePercentage {搞定;组; }
        公众可空<浮动> OfficePercentage {搞定;组; }
    }
}

然后我在不同的目录中创建这个文件:PayrollMarkup_state.cs

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq的;
使用的System.Web;命名空间ACore.Models
{
    [MetadataType(typeof运算(PayrollMarkupMetadata))]
    公共部分类PayrollMarkup_State
    {
    }    公共类PayrollMarkupMetadata
    {
        [UIHint(StatesEditor)]
        公共字符串状态; //必须具有相同的类型和名称模型
    }
}


解决方案

虽然它有点痛苦的,你需要创建一个类作为<一个使用href=\"http://ryanhayes.net/blog/data-annotations-for-entity-framework-4-entities-as-an-mvc-model/\"><$c$c>MetadataType为你的模型类。

  [MetadataType(typeof运算(PayrollMarkupMetadata))
公共部分类PayrollMarkup_State
{
  ...
}公共类PayrollMarkupMetadata
{
    [UIHint(StatesEditor)]
    公共字符串状态; //必须具有相同的类型和名称模型
    //等等。
}

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....

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; }
    }
}

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
    }
}

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆