ToList()抛出异常

时间:2015-07-09 00:10:48

标签: c# asp.net asp.net-mvc-5

我尝试从数据库中检索列表。 但是,当我调用ToList()方法时,它会抛出异常。知道数据库不为空。

 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 Class:

  [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;       }
}
}

任何人都知道如何修复它?

3 个答案:

答案 0 :(得分:1)

我替换了

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)
                    });
                    }

        }

它工作正常

答案 1 :(得分:0)

错误信息非常明确:EF显然会生成一个与......相似的查询。

select ..., rapportAnomalie_code_rapport, ...
from refAnomalie

...错误告诉您rapportAnomalie_code_rapport中没有refAnomalie列。

您需要查看EF为refAnomalie生成的类。在那里的某个地方可能会提到那个神秘的专栏。

答案 2 :(得分:0)

@Omar,改变完整的方法不是问题的解决方案。我看到这个问题的方式是你有导航属性的命名约定问题。有时会混淆EF并导致产生&#34; Guessed&#34;列名不存在。

我建议彻底查看有关导航属性和名称生成的Discussion。另请查看如何使用InverseProperty属性克服此问题。

由于我们没有详细了解您的EF代码第一个实体,因此很难指出问题区域。但希望以上建议可以帮助您重新访问代码并确定问题。