简体   繁体   中英

Pass generic list to stored procedure using ASP.NET MVC and Entity Framework

SqlParameter param = new SqlParameter();
param.ParameterName = "@myList";
param.SqlDbType = SqlDbType.Structured;
param.Value = datatable;
param.Direction = ParameterDirection.Input;
param.TypeName = "tblTempBaseFileInsert";

var outPutList = db.Database.SqlQuery<int>("spInsertToBaseFile @myList", param).FirstOrDefault();

I get an error :

The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types.

The error, perhaps obtusely, is telling you simply that the stored procedure is returning multiple columns of data, but you're trying to cast everything to an int . You need to create a class that maps to what your stored procedure is returning and use that as the generic type param to SqlQuery . For example, if you're getting columns named Foo and Bar back:

public class MyListDto
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

Then:

var outPutList = db.Database.SqlQuery<MyListDto>("spInsertToBaseFile @myList", param).FirstOrDefault();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM