-1

I don´t know if this is the right way to do this. Here is my class generated from Entities 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; }
    }
}

I have created another class for data anottions, as seen 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; }


    }
}

When I try to save in the context this new class Usuario, it says I cant:

The wrong type error

So what is the best way to correct this?

Community
  • 1
  • 1
dipi evil
  • 489
  • 1
  • 11
  • 20

2 Answers2

1

Entity framework is generating the classes for you here based on your Database. Why not add your annotations to the generated class? If you want to use your own POCOs you need to go code first. Depending on what the annotations are for (e.g front end) use a DTO i.e the Class you have written with DTO tacked onto the end of the class name. You can then map that back to the generated class before you save it to the database.

Luthervd
  • 1,388
  • 2
  • 14
  • 25
0

MeteData classes are not meant to be DTOs that can be substituted for the entity type - they are meant for you to have a place to add attributes to your entity model without changing the designer-generated code. You should either use your entity class in your app (not the MetaData class), or use a different model type to use in your app, then map it back to the entity model.

D Stanley
  • 149,601
  • 11
  • 178
  • 240