c# - ToList() throws an exception -


i try retrieve list data base. when call tolist() method throws , exception.knowing database not empty.

 var listeref = r in db.refanomalies                        select r;         rapportanomalie rp = new rapportanomalie();         list<refanomalie> listerefference = listeref.tolist(); 

the exception : {"invalid column name 'rapportanomalie_code_rapport'."}

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

  [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 fix it?

i replaced

var listeref = r in db.refanomalies                    select r; 

with

string sql = @"select * 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 worked fine


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -