实体框架5.0数据注释(数据库第一)(Data Annotations with Entity Fr

2019-07-23 07:49发布

是什么,如果我使用的是实体框架(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
    }
}

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


Answer 2:

你有一个命名空间的问题 - 你已经定义了两个不同的PayrollMarkup_State类,一个在ACORE命名空间下,一个在ACore.Models命名空间下。 包含元数据的类型定义文件中的变化(从ACore.Models)的命名空间来ACORE。



Answer 3:

您可以使用局部元数据类

http://www.asp.net/mvc/overview/getting-started/database-first-development/enhancing-data-validation



Answer 4:

我用了两个额外的类:地图和元,这里是我的地图:

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

    }
}


文章来源: Data Annotations with Entity Framework 5.0 (database first)