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.
Just for the record, the constructor code was missing from the example below
– Jéf Bueno
It is. Thank you.
– Leonel Sanches da Silva
Thank you. Answer me a question, what is the advantage of using Code First?
– user31040
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.
– Leonel Sanches da Silva
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()
 { 
 return View(db.Fornecedores.Where(s => s.Ativo == false).ToList());
 }
db.Fornecedores.Where(s => s.Ativo == false).ToList()
– user31040
This is a little long:
db.Fornecedores.Where(s => s.Ativo == false).ToList()
. Usedb.Fornecedores.Where(s => !s.Ativo).ToList()
. Also check how the columns are in the bank. It seems to be something simple.– Leonel Sanches da Silva
Still wrong. Is my problem in context?
public DbSet<Fornecedor> Fornecedores { get; set; }
– user31040
I don’t know. I would check the base.
– Leonel Sanches da Silva