2015-10-15 211 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

你能举例说明这个不同的模型类型在应用程序中使用吗?谢谢! –

相关问题