【问题标题】:Data Annotations with Entity Framework 5.0 (database first)使用 Entity Framework 5.0 进行数据注释(数据库优先)
【发布时间】:2013-03-25 18:03:07
【问题描述】:

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

这是我由实体框架创建的部分类:

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

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

实体框架生成的文件:'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
    }
}

【问题讨论】:

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


    【解决方案1】:

    虽然有点痛苦,但您需要创建一个类以用作模型类的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.
    }
    

    【讨论】:

    • 如果您在元数据类中声明一个字段而不是一个属性,这真的有效吗?
    • @JYL 是的,它不看是字段还是属性,它只是一种间接机制。 “在具有此类型和名称的其他类中找到这个东西,并将这些属性应用于它。”
    • 感谢 Julie Lerman 将我链接到这篇博文。更多关于使用 GalacticCowboy 方法的细节在这里。 Ardalis - Adding attributes to generated classes
    【解决方案2】:

    您遇到了命名空间问题 - 您定义了两个不同的 PayrollMarkup_State 类,一个在 ACore 命名空间下,一个在 ACore.Models 命名空间下。在包含元数据类型定义的文件中将命名空间更改为 ACore(来自 ACore.Models)。

    【讨论】:

    • 你是对的,但@GalacticComboy 有原来的好答案。
    【解决方案3】:
    【解决方案4】:

    我使用了两个额外的类:Map 和 Meta,这是我的地图:

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

    现在这里是元类:

    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    
    namespace Whatever.Models
    {
        public class ThisMeta
        {
    
            [DisplayName("")]
            public int UID { get; set; }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-13
      • 2016-08-22
      • 1970-01-01
      • 2016-03-31
      • 2020-02-29
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多