2015-10-15 179 views
-1

我不知道這是否是正確的方式。這是我從實體6.XX生成類:將班級另存爲另一個班級

namespace bd.inputdata.edmx 
{ 
    using model; 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 

    [MetadataType(typeof(Usuario))] 
    public partial class input_usuario 
    { 
     public int id { get; set; } 
     public string nome { get; set; } 
     public string usuario { get; set; } 
     public string senha { get; set; } 
     public string email { get; set; } 
     public int id_grupo { get; set; } 
     public System.DateTime data_criacao { get; set; } 
     public System.DateTime data_alteracao { get; set; } 
     public Nullable<int> tipo { get; set; } 
     public byte ativo { get; set; } 
    } 
} 

我已經創造了另一個類的數據anottions,如圖here

using System; 
using bd.inputdata.Base; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace bd.inputdata.model 
{ 
    [Table("usuario")] 
    public class Usuario : IRaizDeAgregacao 
    { 
     [Key] 
     public int id { get; set; } 

     [Required] 
     [StringLength(150)] 
     public string nome { get; set; } 

     [Required] 
     [StringLength(100)] 
     public string usuario { get; set; } 

     [Required] 
     [StringLength(100)] 
     public string senha { get; set; } 

     [Required] 
     [StringLength(50)] 
     public string email { get; set; } 

     [Required] 
     public int id_grupo { get; set; } 

     [Timestamp] 
     public DateTime data_criacao { get; set; } 

     [Timestamp] 
     public DateTime data_alteracao { get; set; } 

     public int? tipo { get; set; } 
     public byte ativo { get; set; } 


    } 
} 

當我試圖在這個新類Usuario,它說我不能上下文保存:

The wrong type error

那麼,什麼是糾正這一點的最好方法是什麼?

+1

嘗試以下操作: 變化'公共類Usuario:IRaizDeAgregacao'到'公共部分類input_usuario:IRaizDeAgregacao' – Eon

+0

@Krohn不能屬性的屬性通過部分類添加。 –

+0

@Krohn你的意思是隻使用相同的類名? –

回答

1

實體框架根據您的數據庫在這裏爲您生成類。爲什麼不把你的註釋添加到生成的類?如果你想使用你自己的POCO,你需要先編碼。根據註釋的用途(例如前端),使用DTO,即您使用DTO編寫的類添加到類名的末尾。然後,您可以將其映射回生成的類,然後將其保存到數據庫中。

0

MeteData類並不意味着是可以替換實體類型的DTO - 它們意味着您可以在不更改設計器生成的代碼的情況下爲實體模型添加屬性。您應該在您的應用中使用實體類(而不是MetaData類),或者在您的應用中使用不同的模型類型,然後將其映射回實體模型。

+0

你能舉例說明這個不同的模型類型在應用程序中使用嗎?謝謝! –

相關問題