c# - Entity Framework 5.0 的数据注释(数据库优先)

标签 c# entity-framework entity-framework-5 data-annotations

如果我使用 Entity Framework (v5.0) 数据库优先方法,使用数据注释进行验证的最佳方法是什么?

这是我通过 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; }
    }
}

我试过了,没有成功....

Entity Framework 生成的文件:'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'

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

关于c# - Entity Framework 5.0 的数据注释(数据库优先),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621656/

相关文章:

c# - 在同一项目中将 EF DbContext 与 mysql 和 sql 一起使用

C#路径问题

c# - 忽略正则表达式匹配的空格

c# - HTMLAgilityPack SelectNodes 选择所有 <img> 元素

c# - 从 HttpResponseMessage.Content 读取流式内容

c# - 数据库表为空时如何从数据库中获取最大ID?

c# - Entity Framework 导航属性上的 OrderBy(一对多)

c# - 在 MySQL 和 Entity Framework 6 的 Where 中使用 TimeSpan 时的错误结果

c# - EntityFramework 长时间运行任务的等待操作超时

web-config - EF 迁移的 Web.config 配置