Was ist der beste Weg, um Datenkommentare für die Validierung zu verwenden, wenn ich eine Entity Framework-Datenbank (v5.0) verwende?
Dies ist meine von Entity Framework erstellte Teilklasse:
//------------------------------------------------------------------------------
// <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; }
}
}
Ich habe es ohne Erfolg versucht ....
Von Entity Framework generierte Datei: '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; }
}
}
Ich habe diese Datei dann in einem anderen Verzeichnis erstellt: '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
}
}
Obwohl es etwas schmerzhaft ist, müssen Sie eine Klasse erstellen, die als MetadataType
für Ihre Modellklasse verwendet wird.
[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.
}
Sie haben ein Namensraumproblem - Sie haben zwei verschiedene PayrollMarkup_State-Klassen definiert, eine unter dem ACore-Namensraum und eine unter dem ACore.Models-Namensraum. Ändern Sie den Namespace in ACore (von ACore.Models) in der Datei, die die Metadatentypdefinition enthält.
Sie können eine partielle Metadaten-Klasse verwenden.
http://www.asp.net/mvc/overview/getting-started/database-first-development/enhancing-data-validation
Ich habe zwei zusätzliche Klassen benutzt: Map und Meta, hier ist meine Map:
namespace Whatever.Models
{
[MetadataType(typeof(ThisMeta))]
public partial class This
{
}
}
hier ist jetzt Meta-Klasse:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Whatever.Models
{
public class ThisMeta
{
[DisplayName("")]
public int UID { get; set; }
}
}