0

I have been stuck on executing a stored procedure through asp.net mvc. I'm using Entity Framework version 5, database first approach. I want to execute a stored parameters whose parameters are passed through a front-end web page.

The stored procedure returns a table with all the columns. The table is StaggingInternal, so the sp returns:ù

select * from stagginginternal.

Using EF, in my asp.net mvc application, I used the edmx designer to get the sp, then in the model browser I selected the return type of the sp as entity. The name of the entity is stagginginternal.

But I get an exception everytime I try to execute the sp. The exception is :

An exception of type 'System.ArgumentException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: No mapping exists from object type System.Data.Entity.Infrastructure.DbQuery1[[<>f__AnonymousTyped1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], NewWeb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to a known managed provider native type.

Code snippet:

var servername = from p in db2.AppClientServerDatabaseRelationships
              join q in db2.AppServers on p.ServerID equals q.ServerID
              where p.clientID == clientid
              select new {  q.ServerName };

var databasename = from p in db2.AppClientServerDatabaseRelationships
               join q in db2.AppDatabaseNames on p.DatabaseID equals q.DatabaseID
               where p.clientID == clientid
               select new { q.DatabaseName };

var datefrom = "01/01/2017";
var dateto = "01/01/2017";

var serverparameter = new SqlParameter("@serverName", servername);
var dbparameter = new SqlParameter("@awDatabase", databasename);
var dtTo = new SqlParameter("@dtTo", dateto);
var dtFrom = new SqlParameter("@dtFrom", datefrom);

List<Object> Parameters = new List<Object>();

Parameters.Add(serverparameter);
Parameters.Add(dbparameter);
Parameters.Add(dtTo);
Parameters.Add(dtFrom);

var result = db2.Database.SqlQuery<StaggingInternal>("exec App_aw_Internal @serverName,@awDatabase,@dtTo,@dtFrom",
  Parameters.ToArray()).ToList<StaggingInternal>();

I'm not sure where I'm going wrong. Any ideas please?

Also, if something is unclear please let me know.

Thank you!

10
  • Are the result sets of linq queries typically reasonable values for Stored Proc parameters? Perhaps servername[0] and databasename[0] would make more sense Commented Feb 13, 2017 at 19:05
  • Possibly. I'm not sure, could you please tell me how I can access the first element of the linq result set. Actually that linq query will always return one result, i.e one server and one database. I tried servername[0] but I cannot apply indexing to an IQueryable. I might try servername.SingleOrDefault(). since it returns only one result
    – newbie
    Commented Feb 13, 2017 at 19:08
  • servername.SingleOrDefault() did not resolve the issue either.
    – newbie
    Commented Feb 13, 2017 at 19:13
  • both databasename and servername are queries you need to force evaluate the values with .Single()/.First()/.etc. To debug things I would just start with hardcoded strings.
    – Pawel
    Commented Feb 13, 2017 at 19:14
  • it would be servername.First().ServerName and databasename .First().DatabaseName Commented Feb 13, 2017 at 19:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.