Linq function does not return List

Asked

Viewed 159 times

1

Hello, I have an application and it does not return a list I select via Linq-to-Sql

The error may 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. He must be doing the pluralization via 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’ve tried putting a [Table("Fornecedor")], but this error appears: The model backing the 'Sistemacontext' context has changed Since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/? Linkid=238269).

At first I wasn’t using the Code First, but I believe that by using some Dataannotations he interprets that I want to use the Code First.

1 answer

1

It’s almost all right. Some adjustments are required.

Remove this:

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

Browsing properties should be initialized by the Entity Framework. Not by you.

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 'Sistemacontext' context has changed Since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/? Linkid=238269).

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

PM> Enable-Migrations

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

PM> Add-Migration Inicial
PM> Update-Database
  • 1

    Just for the record, the constructor code was missing from the example below

  • It is. Thank you.

  • Thank you. Answer me a question, what is the advantage of using Code First?

  • 1

    Several, I would say. Incremental control of the database. Do not use SQL for anything. Mount the database through the application code. I see only advantages.

  • Thank you. He still does not make the selection, but no more Supply. To make sure the problem was in the code: public ActionResult Index()&#xA; { &#xA; return View(db.Fornecedores.Where(s => s.Ativo == false).ToList());&#xA; } db.Fornecedores.Where(s => s.Ativo == false).ToList()

  • This is a little long: db.Fornecedores.Where(s => s.Ativo == false).ToList(). Use db.Fornecedores.Where(s => !s.Ativo).ToList(). Also check how the columns are in the bank. It seems to be something simple.

  • Still wrong. Is my problem in context? public DbSet<Fornecedor> Fornecedores { get; set; }

  • I don’t know. I would check the base.

Show 3 more comments

Browser other questions tagged

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