Function Linq does not return List

1
Hello, I have an application and it does not return a list that I make SELECT via Linq-to-Sql

The error can be in the SELECT that linq-to-sql is doing.

SELECT
[Extent1].[Codigo] AS [Codigo], 
[Extent1].[NomeFantasia] AS [NomeFantasia], 
[Extent1].[RazaoSocial] AS [RazaoSocial], 
[Extent1].[IE] AS [IE], 
[Extent1].[CNPJ] AS [CNPJ], 
[Extent1].[Ativo] AS [Ativo], 
[Extent1].[Fornecedor_Codigo] AS [Fornecedor_Codigo] 
FROM [dbo].[Fornecedors] AS [Extent1]

Notice my last line of code my table calls Fornecedor and not Fornecedors . It should be doing pluralization via the Entity Framework.

Model:

public partial class Fornecedor
{

    public Fornecedor()
    {
        this.Entrada = new HashSet<Entrada>();
        this.Produto = new HashSet<Produto>();
    }

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

    [Required(ErrorMessage="Nome fantasia é obrigatório", AllowEmptyStrings=false)]
    public string NomeFantasia { get; set; }

    [Required(ErrorMessage = "Razão Social é obrigatório", AllowEmptyStrings = false)]
    public string RazaoSocial { get; set; }

    [Required(ErrorMessage = "Inscrição Estadual é obrigatório", AllowEmptyStrings = false)]
    public string IE { get; set; }

    [Required(ErrorMessage = "CNPJ é obrigatório", AllowEmptyStrings = false)]
    public string CNPJ { get; set; }

    public Nullable<bool> Ativo { get; set; }


    public virtual ICollection<Entrada> Entrada { get; set; }
    public virtual ICollection<Produto> Produto { get; set; }

    public virtual ICollection<Fornecedor> CollectionFornecedores { get; set; }
}

My Context:

public class SistemaContext : DbContext
{
    public DbSet<Cliente> Clientes { get; set; }
    public DbSet<Fornecedor> Fornecedores { get; set; }
}

My Controller:

 public ActionResult Index()
 {
    return View(db.Fornecedores.Where(s => s.Ativo == true).ToList());
 }

I have tried to put a [Table("Fornecedor")] , but this error appears: The model backing the 'SystemContext' context has changed since the database was created. Consider using Code First Migrations to update the database ( link ).

In principle I was not using Code First , but I believe that by using some DataAnnotations it interprets that I want to use Code First .

    
asked by anonymous 04.09.2015 / 01:24

1 answer

2

It's almost all right. Some adjustments are needed.

Remove this:

public Fornecedor()
{
    this.Entrada = new HashSet<Entrada>();
    this.Produto = new HashSet<Produto>();
}

Navigation properties must be initialized by the Entity Framework. Not for you.

It would look like this:

[Table("Fornecedor")]
public partial class Fornecedor
{    
    [Key]
    public int Codigo { get; set; }

    [Required(ErrorMessage="Nome fantasia é obrigatório", AllowEmptyStrings=false)]
    public string NomeFantasia { get; set; }

    [Required(ErrorMessage = "Razão Social é obrigatório", AllowEmptyStrings = false)]
    public string RazaoSocial { get; set; }

    [Required(ErrorMessage = "Inscrição Estadual é obrigatório", AllowEmptyStrings = false)]
    public string IE { get; set; }

    [Required(ErrorMessage = "CNPJ é obrigatório", AllowEmptyStrings = false)]
    public string CNPJ { get; set; }

    public Nullable<bool> Ativo { get; set; }


    public virtual ICollection<Entrada> Entrada { get; set; }
    public virtual ICollection<Produto> Produto { get; set; }

    public virtual ICollection<Fornecedor> CollectionFornecedores { get; set; }
}

About this:

  

The model backing the 'SystemContext' context has changed since the database was created. Consider using Code First Migrations to update the database ( link ).

In the Package Manager Console (View> Other Windows & Package Manager Console), type:

PM> Enable-Migrations

Wait for Visual Studio to configure your project, then repeat the steps to generate your Migration again:

PM> Add-Migration Inicial
PM> Update-Database
    
04.09.2015 / 01:35