简体   繁体   中英

VS 2013 bombs out when MVC steps in EF6 call using SQL2014

I have an MVC5 project that is divided into a main XXXX.Site and a XXXX.Data dll that has EF6 connecting to the MS-SQL 2014 database.

When I'm on the MVC controller and press F10 at the call that runs inside the XXXX.Data dll, all goes well. If I go inside the DLL code and put a breakpoint on the actual EF call ...Visual Studio simply bombs out.

I tried different things like re-adding EF6.1.1.1, I on both DLL and MVC site but nothing works. I tried removing and then adding completely EF. I tried a new project and just put the code to run a simple stored proc, I even tried to combine the Data.dll into the MVC Site code moving all DB access to the MVC Site (mainly deleted the DLL itself) ...but nothing worked !!

This is what I noticed so far:

1) if I put a breakpoint on the code I wrote to call this stored procedure ...Visual Studio simply bombs out.

    try
    {
        using (MyDB db = new MyDB())
        {
            // IF BREAKPOINT IS ON LINE BELOW, EXECUTION STOPS ABRUPTLY 
            db.MyStoredProc(value1, value2);
        }
    }
    catch (Exception ex)
    {
        string s = ex.Message;
        return false;
    }

No exception is raised, and when it occurs the browsers kind of flashes a couple of times. Then this message appears on the Output window:
A first chance exception of type 'System.AccessViolationException' occurred in XXXX.Data.dll

2) If instead I put the breakpoint inside the actual auto generated EF6 code, the program does runs normally.

public virtual int MyStoredProc(string value1, string value2)
{
    var value1Parameter = value1 != null ?
        new ObjectParameter("Value1", value1) :
        new ObjectParameter("Value1", typeof(string));

    var value2Parameter = value2 != null ?
        new ObjectParameter("Value2", value2) :
        new ObjectParameter("Value2", typeof(string));

    // IF BREAKPOINT IS ON LINE BELOW, EXECUTION RUNS NORMALLY 
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("MyStoredProc", value1Parameter, value2Parameter);
}

Note that I have both EntityFramework and EntitySQLServer dlls on both bin folders (MVC and DLL).

Questions:
Is it an issue with SQL2014? I don't have this happening when connecting to SQL2012.
Is there a setting that will display the actual exception occurred?
Why is VS bombing out instead of displaying the actual error?

I was getting this "Attempted to read or write protected memory exception" error while using a SQL Server stored procedure that had an output parameter of type 'Date'. I tried various things without success and, in the interest of time, settled on the following solution.

1) Remove the output parameter of type date from the stored procedure.

2) Return a string via a select statement in the stored procedure instead.

SELECT CONVERT(char(10), @AsOfDate, 20) AS AsOfDate

3) Convert the string returned from the stored procedure to a DateTime value in C#.

DateTime asOfDate = DateTime.Now;
using (var context = new DA.MyEntities())
{
    var procResult = context.myStoredProcedure(myParameter).FirstOrDefault();
    DateTime.TryParse(procResult, out asOfDate);
}

I'm not super happy with this compromise, but it did allow me to move forward.

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