How do I not map a Class with the Entity Framework

Asked

Viewed 207 times

1

I’m making my first system (C# ASP MVC 5) with Entity Framework and Migrations.

I have my classes mapped correctly and migrated to the Database that is SQL Server 2012.

But the question is how I can create a class that is not mapped by EF and Migrations?

I have a class of Certificates as below:

In this class that create a Status field, I thought to create a class so that it is an Enum data type, but EF insists on creating this class/table in my database. This Enum would only have the values (Active, Inactive, Expired).

I think then I don’t need to create a physical table in the bank.

What’s the best way for me to do this? Thanks in advance.

using System;
using System.ComponentModel.DataAnnotations;

namespace ControleGCD.Models
{
    public class Certificado
    {

        [Key]
        public int CertificadoId { get; set; }

        public string CertificadoChave { get; set; }

        public string CertificadoDescricao { get; set; }

        public decimal CertificadoPreco { get; set; }

        public DateTime CertificadoDtCompra { get; set; }

        public DateTime CertificadoDtVencimento { get; set; }

        public DateTime CertificadoDtCadastro { get; set; }

       **public DateTime CertificadoStatus{ get; set; }**

    }
}
  • 1

    "is an Enum data type", why not create an Enum?

1 answer

2

You can use the Dataannotation [NotMapped]

namespace ControleGCD.Models
{
    public class Certificado
    {
        //...
        public DateTime CertificadoDtCadastro { get; set; }

        [NotMapped]
        public DateTime CertificadoStatus{ get; set; }
    }  
}
  • 1

    It worked perfectly here, I created a new Enum class (Statuscertified ), and in my existing class (certificates) I created a property of type Statuscertified. You gave it right. Thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.