[英]ToList() throws an exception
I try to retrieve a list from the data base. 我尝试从数据库中检索列表。 However when I call the ToList() method it throws and exception.Knowing that the database is not empty. 但是,当我调用ToList()方法时,它会抛出异常。知道数据库不为空。
var listeRef = from r in db.refAnomalies
select r;
rapportAnomalie rp = new rapportAnomalie();
List<refAnomalie> listeRefference = listeRef.ToList();
The exception : {"Invalid column name 'rapportAnomalie_code_rapport'."}
异常: {"Invalid column name 'rapportAnomalie_code_rapport'."}
this is my database tables: 这是我的数据库表:
CREATE TABLE [dbo].[refAnomalie] (
[code_anomalie] INT IDENTITY (1, 1) NOT NULL,
[libelle_anomalie] NVARCHAR (100) NOT NULL,
[score_anomalie] INT NOT NULL,
[classe_anomalie] NVARCHAR (100) NOT NULL,
CONSTRAINT [PK_dbo.refAnomalie] PRIMARY KEY CLUSTERED ([code_anomalie] ASC)
);
CREATE TABLE [dbo].[rapportAnomalie] (
[code_rapport] INT IDENTITY (1, 1) NOT NULL,
[date_rapport] DATETIME NOT NULL,
[etat] NVARCHAR (50) NOT NULL,
[code_agence] INT NOT NULL,
CONSTRAINT [PK_dbo.rapportAnomalie] PRIMARY KEY CLUSTERED ([code_rapport] ASC),
CONSTRAINT [FK_dbo.rapportAnomalie_dbo.agence_code_agence] FOREIGN KEY ([code_agence]) REFERENCES [dbo].[agence] ([code_agence]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_code_agence]
ON [dbo].[rapportAnomalie]([code_agence] ASC);
rapportAnomalie Class : rapportAnomalie类别:
[Table("rapportAnomalie")]
public partial class rapportAnomalie
{
[Key]
public int code_rapport { get; set; }
public DateTime date_rapport { get; set; }
[Required]
[StringLength(50)]
public string etat { get; set; }
public int code_agence { get; set; }
[ForeignKey("code_agence")]
public agence agence { get; set; }
public List<refAnomalie> listeRefAnomalie { get; set; }
public List<ligneRapportAnomalie> listeLigneRapportAnomalie { get; set; }
}
}
Anyone knows how to fix it? 有人知道如何解决吗?
I replaced 我更换了
var listeRef = from r in db.refAnomalies
select r;
with 同
String sql = @"select * from refAnomalie";
List<refAnomalie> listeRefference = new List<refAnomalie>();
var con = new SqlConnection("data source=(LocalDb)\\MSSQLLocalDB;initial catalog=Banque;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework");
using (var command= new SqlCommand(sql,con)){
con.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
listeRefference.Add(new refAnomalie
{
code_anomalie = reader.GetInt32(0),
libelle_anomalie = reader.GetString(1),
score_anomalie = reader.GetInt32(2),
classe_anomalie = reader.GetString(3)
});
}
}
and it worked fine 而且效果很好
The error message is quite clear: EF evidently generates a query simliar to... 错误消息非常清楚:EF显然会生成与...类似的查询。
select ..., rapportAnomalie_code_rapport, ...
from refAnomalie
...and the error tells you that there is no rapportAnomalie_code_rapport
column in refAnomalie
. ...和错误告诉你,有没有rapportAnomalie_code_rapport
列refAnomalie
。
You need to take a look at the class that EF generates for refAnomalie
. 您需要看一下EF为refAnomalie
生成的类。 There is probably a reference to that mysterious column somewhere in there. 那里的某个地方可能引用了那个神秘的专栏。
@Omar, Changing the complete approach is not the solution of the problem. @Omar,更改完整方法不是解决问题的方法。 The way I see this problem is you're having naming convention issue with Navigation properties. 我看到此问题的方式是,导航属性出现命名约定问题。 Which sometime confuses the EF and results in generating a "Guessed" column name which doesn't exist. 有时会混淆EF并导致生成不存在的“已猜测”列名称。
I would recommend to thoroughly go through this Discussion about navigation property and name generations. 我建议彻底通过这个去讨论有关导航属性和名称代。 Also check how to overcome this using InverseProperty attribute. 还要检查如何使用InverseProperty属性克服此问题 。
Since We don't have complete details of your EF code first entities it's really hard to point out the problem area. 由于我们没有您的EF代码优先实体的完整详细信息,因此很难指出问题所在。 But hope above suggestions will help you re-visit your code and identify the problem. 但是希望以上建议可以帮助您重新访问代码并确定问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.