web-dev-qa-db-ja.com

Entity Framework 5.0を使用したデータ注釈(データベースファースト)

Entity Framework(v5.0)データベースの最初のアプローチを使用している場合、検証にデータ注釈を使用する最良の方法は何ですか?

これは、EntityFrameworkによって作成された私の部分的なクラスです。

//------------------------------------------------------------------------------
// <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
    }
}
22
Mithrilhall

やや面倒ですが、モデルクラスの 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.
}
23
GalacticCowboy

名前空間に問題があります。1つはACore名前空間の下に、もう1つはACore.Models名前空間の下に2つの異なるPayrollMarkup_Stateクラスを定義しました。メタデータタイプ定義を含むファイルで、名前空間を(ACore.Modelsから)ACoreに変更します。

3
Moho
1

MapとMetaの2つの追加クラスを使用しました。これが、私のマップです。

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

    }
}
0
cagedwhale