C#; EF database first mode

126 views Asked by At

I used database-first mode in EF. I need your help because I have a problem when I try to run this code:

private void btnSearch_Click(object sender, EventArgs e)
{
    string SNumber = txtSearchByNum.Text;
   
    var qSearchByTypeText = database.tblNote.SqlQuery("select * From tblNote Where fNumber=@t0", SNumber).ToList();
}

I get this error :

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.Entity.dll

Additional information: Must declare the scalar variable "@t0".

Could this be due to the ٍEntity Framework version?

2

There are 2 answers

4
NajiMakhoul On

you need to pass parameter like:

var qSearchByTypeText = database.tblNote.SqlQuery("select * From tblNote Where fNumber=@t0", new SqlParameter("@t0", SNumber)).ToList();
0
Cwinds On

If you are asking for an EF6.0 "Using" statement to enclose an EF/ORM query that creates and returns a list of rows, here is what I would do:

var qSearchByTypeText = new List<Note>();  //where "Note" is a data structure that looks like a row of columns in "tblNote".

using (var myDbEntity = new [your 'Database First' Entity name]())
{
   //then your query
   qSearchByTypeText = (from n in myDBEntity.tblNotes where n.fNumber == SNumber select n).ToList();

}