我尝试从数据库中检索一个列表。但是,当我调用ToList()方法时,它抛出并exception.Knowing表示数据库不为空。
var listeRef = from r in db.refAnomalies
select r;
rapportAnomalie rp = new rapportAnomalie();
List<refAnomalie> listeRefference = listeRef.ToList();例外:{"Invalid column name 'rapportAnomalie_code_rapport'."}
下面是我的数据库表:
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类:
[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; }
}
}有人知道怎么修吗?
发布于 2015-07-09 08:46:41
我换掉了
var listeRef = from r in db.refAnomalies
select r;使用
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)
});
}
}它工作得很好
发布于 2015-07-09 08:51:45
错误信息非常清楚: EF显然生成了一个类似于...的查询。
select ..., rapportAnomalie_code_rapport, ...
from refAnomalie...and该错误告诉您refAnomalie中没有rapportAnomalie_code_rapport列。
您需要查看EF为refAnomalie生成的类。可能在那里的某个地方引用了那个神秘的专栏。
发布于 2015-07-09 09:46:45
@Omar,改变完整的方法并不是问题的解决方案。在我看来,这个问题是因为你的导航属性有命名约定的问题。这有时会混淆EF,并导致生成一个不存在的“猜测”列名。
我建议仔细阅读这篇关于导航属性和名称生成的Discussion。另请查看如何使用InverseProperty属性克服此问题。
因为我们没有你的EF代码实体的完整细节,所以很难指出问题所在。但希望上面的建议能帮助你重新访问你的代码,并找出问题。
https://stackoverflow.com/questions/31305994
复制相似问题