Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Error: The underlying provider failed on Open.EntityFramework EntityFramework

20.08.2018 (👁252)

 

 

Fehlermeldung:

The underlying provider failed on Open.EntityFramework EntityFramework

 

At System.Data.Entity.Core.EntitiyClient.EntityConnection.Open()

 

Problem

Bei Aufruf über Linq an EF EntityFramework

var data = db.tbl_Scans.FirstOrDefault(s => s.URLRef == sURL);

 

 

Problematisacher  Code

var db = new dbAppData_Context();

 

var data = db.tbl_Scans.FirstOrDefault(s => s.URLRef == sURL);

if (data == null)

{

    //--< Add() >--

    data = new tbl_Scans();

    data.Title = sTitle;

    data.URLRef = sURL;

    data.dtCreated = DateTime.Now;

    data.dtListed = DateTime.Now;

    db.tbl_Scans.Add(data);

    clsSys.fx_Log("added: " + data.Title);

    //--</ Add() >--

}

else

 

 

 

Lösung:

Optimierter Code

Nach dem Aufruf der Datenbank Context Verbindung, sollte man die db.connection zusätzlich prüfen

//< check >

    if (db.Database.Connection.State != System.Data.ConnectionState.Open) db.Database.Connection.Open();

    if (sURL == nullreturn;

    //</ check >

 

 

C# code

public static void Add_or_Update_Record_from_List(string sURL, string sTitle)

{

    //----------< db_Update_Add_ListRecord()>------------

    var db = new dbAppData_Context();

 

    //< check >

    if (db.Database.Connection.State != System.Data.ConnectionState.Open) db.Database.Connection.Open();

    if (sURL == nullreturn;

    //</ check >

 

    try

    {

        var data = db.tbl_Scans.FirstOrDefault(s => s.URLRef == sURL);

        if (data == null)

        {

            //--< Add() >--

            data = new tbl_Scans();

            data.Title = sTitle;

            data.URLRef = sURL;

            data.dtCreated = DateTime.Now;

            data.dtListed = DateTime.Now;

            db.tbl_Scans.Add(data);

            clsSys.fx_Log("added: " + data.Title);

            //--</ Add() >--

        }

        else

        {

            //--< Update() >--

            data.dtListed = DateTime.Now;

            clsSys.fx_Log("updated: " + data.IDScan);

            //--</ Update() >--

        }

        db.SaveChanges();

    }

    catch (Exception ex)

    {

        clsSys.fx_Error_Log("clsdb error: " + ex.Message + Environment.NewLine + "url=" + sURL);

    }

    

    //----------</ db_Update_Add_ListRecord()>------------

}