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 .