web-dev-qa-db-fra.com

Annotations de données avec Entity Framework 5.0 (base de données en premier)

Quel est le meilleur moyen d'utiliser les annotations de données pour la validation si j'utilise une première approche de base de données Entity Framework (v5.0)?

Ceci est ma classe partielle créée par 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; }
    }
}

J'ai essayé sans succès ....

Fichier généré par 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; }
    }
}

J'ai ensuite créé ce fichier dans un répertoire différent: '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
    }
}
21
Mithrilhall

Bien que cela soit un peu pénible, vous devez créer une classe à utiliser en tant que MetadataType pour votre classe de modèle.

[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.
}
22
GalacticCowboy

Vous avez un problème d'espace de nom - vous avez défini deux classes PayrollMarkup_State différentes, une sous l'espace de noms ACore et l'autre sous l'espace de noms ACore.Models. Changez l'espace de noms en ACore (à partir d'ACore.Models) dans le fichier contenant la définition du type de métadonnées.

2
Moho
1
Shivam Srivastava

J'ai utilisé deux classes supplémentaires: Map et Meta, voici ma carte:

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


}

maintenant voici meta class:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Whatever.Models
{
    public class ThisMeta
    {

        [DisplayName("")]
        public int UID { get; set; }

    }
}
0
cagedwhale