2013-03-25 23 views
21

Bir Varlık Çerçevesi (v5.0) veritabanı ilk yaklaşımını kullanıyorum, doğrulama için veri ek açıklamalarını kullanmanın en iyi yolu nedir?Entity Framework 5.0 ile Veri Ek Açıklamaları (önce veritabanı)

Bu Varlık Framework benim kısmi sınıf oluşturulur: '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; } 
    } 
} 
:

varlık Framework dosyayı oluşturan

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

hiçbir başarı ile denedim ....

Bu dosyayı farklı bir dizinde oluşturdum: '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 
    } 
} 

cevap

21

Biraz ağrılı olmasına rağmen, model sınıfınız için MetadataType olarak kullanmak üzere bir sınıf oluşturmanız gerekir.

[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. 
} 
+0

Bildirirseniz bu gerçekten işe yarar meta veri sınıfınızda bir mülk yerine bir alan mı? – JYL

+2

@JYL evet, bir alan ya da mülk olup olmadığına bakmaz, sadece bir dolaylı mekanizmadır. "Bu şeyi başka bir sınıfta bu tür ve adla bulun ve bu öznitelikleri buna uygulayın." – GalacticCowboy

+0

Ok teşekkürler! +1. – JYL

2

Bir ad alanı sorunu - İki farklı PayrollMarkup_State sınıfları, ACore ad altında bir ve ACore.Models ad altında tanýmladýysanýz. Meta veri türü tanımını içeren dosyada ad alanını ACore (ACore.Models'den) olarak değiştirin.

+0

Haklısınız, ama @GalacticComboy orijinal iyi cevabı vardır. – JYL

0

sınıfını kullanabilirsiniz: şimdi burada

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


} 

meta sınıftır: Harita ve Meta, işte benim haritasıdır:

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

namespace Whatever.Models 
{ 
    public class ThisMeta 
    { 

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

    } 
}