c# Entity Framework: An error occurred accessing the database












0















I have an existing application which uses mysql as database. and it's working correctly.



But now. I need to create a version of this. In-house and for home based.



My problem is when i update a feature or add a feature in the in-house program i also need to update the home based, the home based uses sqlite database.



Stored procedure doesn't work in sqlite. So idecided to use entity framework. So i can easily change database. I already create a model for one of my tables



This contains the given names for auto suggestion of my application.



And here is my class



class Given
{
public int id { get; set; }
public string name { get; set; }
public string @operator { get; set; }
public DateTime data_time { get; set; }
}


And i used it like this



class MyDb : DbContext
{
public MyDb() : base("name=sample")
{

}
public DbSet<Given> tble_given { get; set; }
}


i named it tble_given because it's the name of my table in the database



And this is my connection string in the App.config file



<connectionStrings>

<add name="sample" providerName="MySql.Data.MySqlClient" connectionString="server=127.0.0.1; uid=root; pwd=; database=db_sample;default command timeout=20;" />
</connectionStrings>


And this is my code for accessing the data in the database



                using (var db = new MyDb())
{
db.Database.Connection.Open();
Debug.WriteLine(db.Database.Connection.State);
foreach (var item in db.tble_given.ToList())
{

}
}


I added the connection open and the connection state in order to check if the connection is valid.



but when i query it.



added the code db.tble_given.ToList()



Exception thrown: 'System.Data.Entity.Core.ProviderIncompatibleException' in Guatemala Coding.exe
An exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in Guatemala Coding.exe but was not handled in user code
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.



I tried solving it myself but i can't make this code work.



Why do i have this error if the connection state is open?



Thank you










share|improve this question



























    0















    I have an existing application which uses mysql as database. and it's working correctly.



    But now. I need to create a version of this. In-house and for home based.



    My problem is when i update a feature or add a feature in the in-house program i also need to update the home based, the home based uses sqlite database.



    Stored procedure doesn't work in sqlite. So idecided to use entity framework. So i can easily change database. I already create a model for one of my tables



    This contains the given names for auto suggestion of my application.



    And here is my class



    class Given
    {
    public int id { get; set; }
    public string name { get; set; }
    public string @operator { get; set; }
    public DateTime data_time { get; set; }
    }


    And i used it like this



    class MyDb : DbContext
    {
    public MyDb() : base("name=sample")
    {

    }
    public DbSet<Given> tble_given { get; set; }
    }


    i named it tble_given because it's the name of my table in the database



    And this is my connection string in the App.config file



    <connectionStrings>

    <add name="sample" providerName="MySql.Data.MySqlClient" connectionString="server=127.0.0.1; uid=root; pwd=; database=db_sample;default command timeout=20;" />
    </connectionStrings>


    And this is my code for accessing the data in the database



                    using (var db = new MyDb())
    {
    db.Database.Connection.Open();
    Debug.WriteLine(db.Database.Connection.State);
    foreach (var item in db.tble_given.ToList())
    {

    }
    }


    I added the connection open and the connection state in order to check if the connection is valid.



    but when i query it.



    added the code db.tble_given.ToList()



    Exception thrown: 'System.Data.Entity.Core.ProviderIncompatibleException' in Guatemala Coding.exe
    An exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in Guatemala Coding.exe but was not handled in user code
    An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.



    I tried solving it myself but i can't make this code work.



    Why do i have this error if the connection state is open?



    Thank you










    share|improve this question

























      0












      0








      0








      I have an existing application which uses mysql as database. and it's working correctly.



      But now. I need to create a version of this. In-house and for home based.



      My problem is when i update a feature or add a feature in the in-house program i also need to update the home based, the home based uses sqlite database.



      Stored procedure doesn't work in sqlite. So idecided to use entity framework. So i can easily change database. I already create a model for one of my tables



      This contains the given names for auto suggestion of my application.



      And here is my class



      class Given
      {
      public int id { get; set; }
      public string name { get; set; }
      public string @operator { get; set; }
      public DateTime data_time { get; set; }
      }


      And i used it like this



      class MyDb : DbContext
      {
      public MyDb() : base("name=sample")
      {

      }
      public DbSet<Given> tble_given { get; set; }
      }


      i named it tble_given because it's the name of my table in the database



      And this is my connection string in the App.config file



      <connectionStrings>

      <add name="sample" providerName="MySql.Data.MySqlClient" connectionString="server=127.0.0.1; uid=root; pwd=; database=db_sample;default command timeout=20;" />
      </connectionStrings>


      And this is my code for accessing the data in the database



                      using (var db = new MyDb())
      {
      db.Database.Connection.Open();
      Debug.WriteLine(db.Database.Connection.State);
      foreach (var item in db.tble_given.ToList())
      {

      }
      }


      I added the connection open and the connection state in order to check if the connection is valid.



      but when i query it.



      added the code db.tble_given.ToList()



      Exception thrown: 'System.Data.Entity.Core.ProviderIncompatibleException' in Guatemala Coding.exe
      An exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in Guatemala Coding.exe but was not handled in user code
      An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.



      I tried solving it myself but i can't make this code work.



      Why do i have this error if the connection state is open?



      Thank you










      share|improve this question














      I have an existing application which uses mysql as database. and it's working correctly.



      But now. I need to create a version of this. In-house and for home based.



      My problem is when i update a feature or add a feature in the in-house program i also need to update the home based, the home based uses sqlite database.



      Stored procedure doesn't work in sqlite. So idecided to use entity framework. So i can easily change database. I already create a model for one of my tables



      This contains the given names for auto suggestion of my application.



      And here is my class



      class Given
      {
      public int id { get; set; }
      public string name { get; set; }
      public string @operator { get; set; }
      public DateTime data_time { get; set; }
      }


      And i used it like this



      class MyDb : DbContext
      {
      public MyDb() : base("name=sample")
      {

      }
      public DbSet<Given> tble_given { get; set; }
      }


      i named it tble_given because it's the name of my table in the database



      And this is my connection string in the App.config file



      <connectionStrings>

      <add name="sample" providerName="MySql.Data.MySqlClient" connectionString="server=127.0.0.1; uid=root; pwd=; database=db_sample;default command timeout=20;" />
      </connectionStrings>


      And this is my code for accessing the data in the database



                      using (var db = new MyDb())
      {
      db.Database.Connection.Open();
      Debug.WriteLine(db.Database.Connection.State);
      foreach (var item in db.tble_given.ToList())
      {

      }
      }


      I added the connection open and the connection state in order to check if the connection is valid.



      but when i query it.



      added the code db.tble_given.ToList()



      Exception thrown: 'System.Data.Entity.Core.ProviderIncompatibleException' in Guatemala Coding.exe
      An exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in Guatemala Coding.exe but was not handled in user code
      An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.



      I tried solving it myself but i can't make this code work.



      Why do i have this error if the connection state is open?



      Thank you







      c# mysql entity-framework






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 4:21









      Ramon bihonRamon bihon

      347




      347
























          1 Answer
          1






          active

          oldest

          votes


















          0














          For every database, there will be a separate nuget package. In your case, both of your databases might have different nuget packages. So you need to take nuget package compatible with your database.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455113%2fc-sharp-entity-framework-an-error-occurred-accessing-the-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            For every database, there will be a separate nuget package. In your case, both of your databases might have different nuget packages. So you need to take nuget package compatible with your database.






            share|improve this answer




























              0














              For every database, there will be a separate nuget package. In your case, both of your databases might have different nuget packages. So you need to take nuget package compatible with your database.






              share|improve this answer


























                0












                0








                0







                For every database, there will be a separate nuget package. In your case, both of your databases might have different nuget packages. So you need to take nuget package compatible with your database.






                share|improve this answer













                For every database, there will be a separate nuget package. In your case, both of your databases might have different nuget packages. So you need to take nuget package compatible with your database.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 4:55









                raja6791raja6791

                11




                11






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455113%2fc-sharp-entity-framework-an-error-occurred-accessing-the-database%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks