[英]C# code-first EF 6. Invalid Column Name
When I try to get data from the table Anexos
, I get this Exeption: 当我尝试从表
Anexos
获取数据时,我得到了这个Exeption:
"An error occurred while executing the command definition. See the inner exception for details."
“执行命令定义时发生错误。有关详细信息,请参阅内部异常。”
"Invalid column name 'Empresas_Id'."
“无效的列名'Empresas_Id'。”
Then I was looking for the Empresas_Id
field in my model. 然后我在我的模型中寻找
Empresas_Id
字段。 But.. 但..
public class Anexos
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Display(Name = "Descrição"), StringLength(150)]
public string Descricao { get; set; }
[StringLength(90)]
public string Nome { get; set; }
public string Caminho { get; set; }
[Column("PessoaId")]
public int? PessoaId { get; set; }
[Column("Contrato_Id")]
public int? ContratoId { get; set; }
[Column("TipoDocumento_Id")]
public int TipoDocumentoId { get; set; }
[ForeignKey("ContratoId")]
public virtual Contratos Contrato { get; set; }
[ForeignKey("PessoaId")]
public virtual Pessoas Pessoa { get; set; }
[ForeignKey("TipoDocumentoId")]
public virtual TipoDocumento TipoDocumento { get; set; }
}
There is no Empresas_Id
field, then I checked my database table, but nothing there too... The Contratos
model have the field, but I can take the data from it without errors. 没有
Empresas_Id
字段,然后我检查了我的数据库表,但是没有任何东西...... Contratos
模型有字段,但我可以从中获取数据而没有错误。 Is this a bug of EF ? 这是EF的错误吗?
I checked this answers trying to find my issue: 我检查了这个答案,试图找到我的问题:
EF Code First “Invalid column name 'Discriminator'” but no inheritance EF Code First“无效的列名'Discriminator'”但没有继承
Entity Framework - Invalid Column Name '*_ID" 实体框架 - 无效的列名称'* _ID“
EF Code first invalid column name “CategoryCategoryID” EF Code第一个无效列名“CategoryCategoryID”
PS: All of my migrations are up-to-date. PS:我的所有迁移都是最新的。
My table Script : 我的表脚本:
CREATE TABLE [dbo].[Anexos] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Descricao] NVARCHAR (150) NULL,
[Nome] NVARCHAR (90) NULL,
[Caminho] NVARCHAR (MAX) NULL,
[PessoaId] INT NULL,
[Contrato_Id] INT NULL,
[TipoDocumento_Id] INT NOT NULL,
CONSTRAINT [PK_dbo.Anexos] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.Anexos_dbo.Contratos_Contrato_Id] FOREIGN KEY ([Contrato_Id]) REFERENCES [dbo].[Contratos] ([Id]),
CONSTRAINT [FK_dbo.Anexos_dbo.Pessoas_PessoaId] FOREIGN KEY ([PessoaId]) REFERENCES [dbo].[Pessoas] ([Id]),
CONSTRAINT [FK_dbo.Anexos_dbo.TipoDocumento_TipoDocumento_Id] FOREIGN KEY ([TipoDocumento_Id]) REFERENCES [dbo].[TipoDocumento] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_PessoaId]
ON [dbo].[Anexos]([PessoaId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Contrato_Id]
ON [dbo].[Anexos]([Contrato_Id] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_TipoDocumento_Id]
ON [dbo].[Anexos]([TipoDocumento_Id] ASC);
My connection string : 我的连接字符串:
<add name="DefaultConnection" connectionString="data source=PC-TREINA09\SQLEXPRESS;initial catalog=erp10_new;integrated security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
The Contratos
table: Contratos
表:
public class Contratos
{
public Contratos()
{
Items = new HashSet<ContratoItens>();
Anexos = new HashSet<Anexos>();
Contas = new HashSet<ContasContasReceber>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int Numero { get; set; }
public DateTime? DataContrato { get; set; }
public DateTime? DataVenda { get; set; }
public int? DiaDoMesCobranca { get; set; }
public int? MesesVigencia { get; set; }
public string Observacoes { get; set; }
public TipoDesconto? TipoDesconto { get; set; }
public double? ValorDesconto { get; set; }
public double? ValorTotal { get; set; }
public StatusContrato Status { get; set; }
[Column("EmpresaId")]
public int EmpresaId { get; set; }
[Column("Cliente_Id")]
public int ClienteId { get; set; }
[Column("ClienteFaturamento_Id")]
public int ClienteFaturamentoId { get; set; }
[Column("Pagamento_Id")]
public int? PagamentoId { get; set; }
[ForeignKey("EmpresaId")]
public virtual Empresas Empresa { get; set; }
[ForeignKey("ClienteId")]
public virtual PessoasCliente Cliente { get; set; }
[ForeignKey("ClienteFaturamentoId")]
public virtual PessoasCliente ClienteFaturamento { get; set; }
[ForeignKey("PagamentoId")]
public virtual Pagamentos Pagamento { get; set; }
public virtual ICollection<Anexos> Anexos { get; set; }
public virtual ICollection<ContratoItens> Items { get; set; }
public virtual ICollection<ContasContasReceber> Contas { get; set; }
}
So, I don't know why but, when a send to my GridView as a List<T>
model it gives me that error, but if I send using AsEnumarable
instead of ToList
, it works: 所以,我不知道为什么,但是,当一个发送到我的GridView作为
List<T>
模型它给我错误,但如果我发送使用AsEnumarable
而不是ToList
,它的工作原理:
public ActionResult IndexContratos(int id)
{
var anexos = db.Anexos
.Include(a => a.Contrato)
.Include(a => a.Pessoa)
.Include(a => a.TipoDocumento)
.Where(x => x.ContratoId == id);
AnexosContratoTemps(id);
return View("Grid", anexos.AsEnumerable());//Works !!
//return View("Grid", anexos.ToList()); //Invalid column name...
}
My View model is: 我的视图模型是:
@model IEnumerable<Anexos>
But a List<T>
is a IEnumarable<T>
, I really don't understand that why ToList
gives me that error, can someone explain? 但是
List<T>
是一个IEnumarable<T>
,我真的不明白为什么ToList
给我那个错误,有人可以解释一下吗?
Finally(by coincidence) I found what was that bug, I forgot to say that the Anexos
property in MyContext
class was virtual, something like this: 最后(巧合)我发现了那个bug,我忘了说
MyContext
类中的Anexos
属性是虚拟的,类似这样:
public DbSet<Anexos> Anexos { get; set; } //WRONG!
public virtual DbSet<Anexos> Anexos { get; set; }//YAYYY!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.