Entity Framework: The context is being used in Code First mode with code that was generated from an EDMX file












12















I am developing an WPF application with EF 6 database first approach, I am have 1 project in my solutions, if i run my project this error always appear.



The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715










share|improve this question























  • Did you set up your connection string? If not have a look here: asp.net/mvc/overview/getting-started/introduction/…

    – Maximilian Ast
    May 30 '16 at 14:46











  • don't work for me.

    – Munjani375
    May 30 '16 at 15:01











  • More than one context per connection string or connection string in wrong format

    – Mozgovoy Artem
    May 30 '16 at 15:37













  • this my connection string

    – Munjani375
    May 30 '16 at 16:06











  • <connectionStrings> <add name="sampleEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.sqlexpress;initial catalog=sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings> </configuration>

    – Munjani375
    May 30 '16 at 16:06
















12















I am developing an WPF application with EF 6 database first approach, I am have 1 project in my solutions, if i run my project this error always appear.



The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715










share|improve this question























  • Did you set up your connection string? If not have a look here: asp.net/mvc/overview/getting-started/introduction/…

    – Maximilian Ast
    May 30 '16 at 14:46











  • don't work for me.

    – Munjani375
    May 30 '16 at 15:01











  • More than one context per connection string or connection string in wrong format

    – Mozgovoy Artem
    May 30 '16 at 15:37













  • this my connection string

    – Munjani375
    May 30 '16 at 16:06











  • <connectionStrings> <add name="sampleEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.sqlexpress;initial catalog=sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings> </configuration>

    – Munjani375
    May 30 '16 at 16:06














12












12








12


3






I am developing an WPF application with EF 6 database first approach, I am have 1 project in my solutions, if i run my project this error always appear.



The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715










share|improve this question














I am developing an WPF application with EF 6 database first approach, I am have 1 project in my solutions, if i run my project this error always appear.



The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715







c# .net wpf entity-framework visual-studio






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 30 '16 at 14:43









Munjani375Munjani375

61114




61114













  • Did you set up your connection string? If not have a look here: asp.net/mvc/overview/getting-started/introduction/…

    – Maximilian Ast
    May 30 '16 at 14:46











  • don't work for me.

    – Munjani375
    May 30 '16 at 15:01











  • More than one context per connection string or connection string in wrong format

    – Mozgovoy Artem
    May 30 '16 at 15:37













  • this my connection string

    – Munjani375
    May 30 '16 at 16:06











  • <connectionStrings> <add name="sampleEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.sqlexpress;initial catalog=sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings> </configuration>

    – Munjani375
    May 30 '16 at 16:06



















  • Did you set up your connection string? If not have a look here: asp.net/mvc/overview/getting-started/introduction/…

    – Maximilian Ast
    May 30 '16 at 14:46











  • don't work for me.

    – Munjani375
    May 30 '16 at 15:01











  • More than one context per connection string or connection string in wrong format

    – Mozgovoy Artem
    May 30 '16 at 15:37













  • this my connection string

    – Munjani375
    May 30 '16 at 16:06











  • <connectionStrings> <add name="sampleEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.sqlexpress;initial catalog=sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings> </configuration>

    – Munjani375
    May 30 '16 at 16:06

















Did you set up your connection string? If not have a look here: asp.net/mvc/overview/getting-started/introduction/…

– Maximilian Ast
May 30 '16 at 14:46





Did you set up your connection string? If not have a look here: asp.net/mvc/overview/getting-started/introduction/…

– Maximilian Ast
May 30 '16 at 14:46













don't work for me.

– Munjani375
May 30 '16 at 15:01





don't work for me.

– Munjani375
May 30 '16 at 15:01













More than one context per connection string or connection string in wrong format

– Mozgovoy Artem
May 30 '16 at 15:37







More than one context per connection string or connection string in wrong format

– Mozgovoy Artem
May 30 '16 at 15:37















this my connection string

– Munjani375
May 30 '16 at 16:06





this my connection string

– Munjani375
May 30 '16 at 16:06













<connectionStrings> <add name="sampleEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.sqlexpress;initial catalog=sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings> </configuration>

– Munjani375
May 30 '16 at 16:06





<connectionStrings> <add name="sampleEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.sqlexpress;initial catalog=sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings> </configuration>

– Munjani375
May 30 '16 at 16:06












8 Answers
8






active

oldest

votes


















13














My mistake was using standard connection string in constructor



(Server = testtest; Database = DB; User Id = test_user;Password = test),



but Entity Framework needs different format



(metadata=res:///DBModel.csdl|res:///DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=testtest;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient)






share|improve this answer































    8














    EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you receive this error you can add the metadata section to the connection string in your config file.



    E.g. if your connection string looks like this:



        <add name="MyModel" connectionString="data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />


    Prepend metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl; so that it looks like this:



    <add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />





    share|improve this answer































      3














      One thing you can do is... (if is Database first)



      Open the .edmx[Diagram] -> right click -> "Update Model from database"



      And see if the will appear the "Add", "Refresh" and "Delete" tabs.



      If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)






      share|improve this answer
























      • What if this is design first?

        – Nilay Vishwakarma
        Aug 1 '17 at 7:22











      • Are you using .Edmx in your project?

        – Rafael Ribeiro
        Aug 1 '17 at 14:11



















      1














      You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:



      "data source=localhostsqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"





      share|improve this answer































        1














        Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api.
        My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:




        An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.




        what i did was: I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string.
        This solved my problem.






        share|improve this answer


























        • Could you post a code example of this, I'm not sure what you mean.

          – Hunter Nelson
          Feb 5 '18 at 0:59











        • @HunterNelson I have already posted in my answer to which i have given the reference as well.

          – Harry .Naeem
          Feb 6 '18 at 5:09



















        0














        I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".



        The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.



        Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!






        share|improve this answer































          0














          I have two projects:



          One is for the generated EDMX file and all related models.



          The other one is the ASP.NET MVC Web.



          I encountered this issue since the connection string that I am using on the ASP.NET MVC Web project is the normal string that I use using ADO.NET connection. So what I did is the following:




          • Open the app.config on your EDMX project files.


          • Copy its connection string.


          • Paste it on the WEB project since this will be used when you start
            the application.







          share|improve this answer































            -2














            Add metadata="res:///MyModel.csdl|res:///MyModel.ssdl|res://*/MyModel.msl" to connectionstring and providerName="System.Data.EntityClient" will resolve the issue






            share|improve this answer



















            • 1





              This question really doesn't need yet another answer stating this.

              – Gert Arnold
              Jul 28 '18 at 13:58











            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%2f37528405%2fentity-framework-the-context-is-being-used-in-code-first-mode-with-code-that-wa%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            13














            My mistake was using standard connection string in constructor



            (Server = testtest; Database = DB; User Id = test_user;Password = test),



            but Entity Framework needs different format



            (metadata=res:///DBModel.csdl|res:///DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=testtest;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient)






            share|improve this answer




























              13














              My mistake was using standard connection string in constructor



              (Server = testtest; Database = DB; User Id = test_user;Password = test),



              but Entity Framework needs different format



              (metadata=res:///DBModel.csdl|res:///DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=testtest;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient)






              share|improve this answer


























                13












                13








                13







                My mistake was using standard connection string in constructor



                (Server = testtest; Database = DB; User Id = test_user;Password = test),



                but Entity Framework needs different format



                (metadata=res:///DBModel.csdl|res:///DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=testtest;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient)






                share|improve this answer













                My mistake was using standard connection string in constructor



                (Server = testtest; Database = DB; User Id = test_user;Password = test),



                but Entity Framework needs different format



                (metadata=res:///DBModel.csdl|res:///DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=testtest;initial catalog=DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""" providerName = ""System.Data.EntityClient)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 28 '17 at 7:20









                Stepan TripalStepan Tripal

                13115




                13115

























                    8














                    EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you receive this error you can add the metadata section to the connection string in your config file.



                    E.g. if your connection string looks like this:



                        <add name="MyModel" connectionString="data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />


                    Prepend metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl; so that it looks like this:



                    <add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />





                    share|improve this answer




























                      8














                      EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you receive this error you can add the metadata section to the connection string in your config file.



                      E.g. if your connection string looks like this:



                          <add name="MyModel" connectionString="data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />


                      Prepend metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl; so that it looks like this:



                      <add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />





                      share|improve this answer


























                        8












                        8








                        8







                        EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you receive this error you can add the metadata section to the connection string in your config file.



                        E.g. if your connection string looks like this:



                            <add name="MyModel" connectionString="data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />


                        Prepend metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl; so that it looks like this:



                        <add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />





                        share|improve this answer













                        EF makes assumptions based on the presence or absence of a metadata section in the connection string. If you receive this error you can add the metadata section to the connection string in your config file.



                        E.g. if your connection string looks like this:



                            <add name="MyModel" connectionString="data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />


                        Prepend metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl; so that it looks like this:



                        <add name="MyModel" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;data source=SERVERINSTANCE;initial catalog=MyModel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 17 '17 at 8:50









                        Daniel de ZwaanDaniel de Zwaan

                        2,3892021




                        2,3892021























                            3














                            One thing you can do is... (if is Database first)



                            Open the .edmx[Diagram] -> right click -> "Update Model from database"



                            And see if the will appear the "Add", "Refresh" and "Delete" tabs.



                            If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)






                            share|improve this answer
























                            • What if this is design first?

                              – Nilay Vishwakarma
                              Aug 1 '17 at 7:22











                            • Are you using .Edmx in your project?

                              – Rafael Ribeiro
                              Aug 1 '17 at 14:11
















                            3














                            One thing you can do is... (if is Database first)



                            Open the .edmx[Diagram] -> right click -> "Update Model from database"



                            And see if the will appear the "Add", "Refresh" and "Delete" tabs.



                            If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)






                            share|improve this answer
























                            • What if this is design first?

                              – Nilay Vishwakarma
                              Aug 1 '17 at 7:22











                            • Are you using .Edmx in your project?

                              – Rafael Ribeiro
                              Aug 1 '17 at 14:11














                            3












                            3








                            3







                            One thing you can do is... (if is Database first)



                            Open the .edmx[Diagram] -> right click -> "Update Model from database"



                            And see if the will appear the "Add", "Refresh" and "Delete" tabs.



                            If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)






                            share|improve this answer













                            One thing you can do is... (if is Database first)



                            Open the .edmx[Diagram] -> right click -> "Update Model from database"



                            And see if the will appear the "Add", "Refresh" and "Delete" tabs.



                            If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead. =)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 30 '16 at 17:57









                            Rafael RibeiroRafael Ribeiro

                            1687




                            1687













                            • What if this is design first?

                              – Nilay Vishwakarma
                              Aug 1 '17 at 7:22











                            • Are you using .Edmx in your project?

                              – Rafael Ribeiro
                              Aug 1 '17 at 14:11



















                            • What if this is design first?

                              – Nilay Vishwakarma
                              Aug 1 '17 at 7:22











                            • Are you using .Edmx in your project?

                              – Rafael Ribeiro
                              Aug 1 '17 at 14:11

















                            What if this is design first?

                            – Nilay Vishwakarma
                            Aug 1 '17 at 7:22





                            What if this is design first?

                            – Nilay Vishwakarma
                            Aug 1 '17 at 7:22













                            Are you using .Edmx in your project?

                            – Rafael Ribeiro
                            Aug 1 '17 at 14:11





                            Are you using .Edmx in your project?

                            – Rafael Ribeiro
                            Aug 1 '17 at 14:11











                            1














                            You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:



                            "data source=localhostsqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"





                            share|improve this answer




























                              1














                              You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:



                              "data source=localhostsqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"





                              share|improve this answer


























                                1












                                1








                                1







                                You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:



                                "data source=localhostsqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"





                                share|improve this answer













                                You shouldnt use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:



                                "data source=localhostsqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 30 '16 at 16:51









                                Mozgovoy ArtemMozgovoy Artem

                                1,34011016




                                1,34011016























                                    1














                                    Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api.
                                    My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:




                                    An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.




                                    what i did was: I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string.
                                    This solved my problem.






                                    share|improve this answer


























                                    • Could you post a code example of this, I'm not sure what you mean.

                                      – Hunter Nelson
                                      Feb 5 '18 at 0:59











                                    • @HunterNelson I have already posted in my answer to which i have given the reference as well.

                                      – Harry .Naeem
                                      Feb 6 '18 at 5:09
















                                    1














                                    Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api.
                                    My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:




                                    An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.




                                    what i did was: I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string.
                                    This solved my problem.






                                    share|improve this answer


























                                    • Could you post a code example of this, I'm not sure what you mean.

                                      – Hunter Nelson
                                      Feb 5 '18 at 0:59











                                    • @HunterNelson I have already posted in my answer to which i have given the reference as well.

                                      – Harry .Naeem
                                      Feb 6 '18 at 5:09














                                    1












                                    1








                                    1







                                    Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api.
                                    My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:




                                    An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.




                                    what i did was: I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string.
                                    This solved my problem.






                                    share|improve this answer















                                    Very much late but still helpful. I got stuck in a similar problem. Posted a question about on SO and was able to find a solution. You can refer to Connection String errors in C# Web Api.
                                    My situation was I had two connection strings in web.config (you'll get to know why when go to the link). Commenting one string was raising the error you got while commenting the other one was raising error as below:




                                    An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.




                                    what i did was: I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string.
                                    This solved my problem.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 2 '17 at 12:50

























                                    answered Nov 2 '17 at 11:11









                                    Harry .NaeemHarry .Naeem

                                    4682522




                                    4682522













                                    • Could you post a code example of this, I'm not sure what you mean.

                                      – Hunter Nelson
                                      Feb 5 '18 at 0:59











                                    • @HunterNelson I have already posted in my answer to which i have given the reference as well.

                                      – Harry .Naeem
                                      Feb 6 '18 at 5:09



















                                    • Could you post a code example of this, I'm not sure what you mean.

                                      – Hunter Nelson
                                      Feb 5 '18 at 0:59











                                    • @HunterNelson I have already posted in my answer to which i have given the reference as well.

                                      – Harry .Naeem
                                      Feb 6 '18 at 5:09

















                                    Could you post a code example of this, I'm not sure what you mean.

                                    – Hunter Nelson
                                    Feb 5 '18 at 0:59





                                    Could you post a code example of this, I'm not sure what you mean.

                                    – Hunter Nelson
                                    Feb 5 '18 at 0:59













                                    @HunterNelson I have already posted in my answer to which i have given the reference as well.

                                    – Harry .Naeem
                                    Feb 6 '18 at 5:09





                                    @HunterNelson I have already posted in my answer to which i have given the reference as well.

                                    – Harry .Naeem
                                    Feb 6 '18 at 5:09











                                    0














                                    I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".



                                    The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.



                                    Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!






                                    share|improve this answer




























                                      0














                                      I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".



                                      The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.



                                      Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!






                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".



                                        The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.



                                        Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!






                                        share|improve this answer













                                        I also faced this exact same message, but workig with a web MVC project. This message is fired when I try to auto generate the Controller from the imported model. It seems that it is not working because "that was generated from an EDMX file".



                                        The good news is that it works if I generate the model based in the "Code First" instead of "EF Designer". The bad news is that I can't use the EF Designer if I want that the automatically controller generation works. Does not matter which from those two ways you generates your model. Once the model is generated, you use it in the same way.



                                        Tries to remove all your emdx objects from your project and recreate the model based in the Code First instead of EF Designer. Worked for me!







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered May 18 '17 at 20:29









                                        danilocgsilvadanilocgsilva

                                        287




                                        287























                                            0














                                            I have two projects:



                                            One is for the generated EDMX file and all related models.



                                            The other one is the ASP.NET MVC Web.



                                            I encountered this issue since the connection string that I am using on the ASP.NET MVC Web project is the normal string that I use using ADO.NET connection. So what I did is the following:




                                            • Open the app.config on your EDMX project files.


                                            • Copy its connection string.


                                            • Paste it on the WEB project since this will be used when you start
                                              the application.







                                            share|improve this answer




























                                              0














                                              I have two projects:



                                              One is for the generated EDMX file and all related models.



                                              The other one is the ASP.NET MVC Web.



                                              I encountered this issue since the connection string that I am using on the ASP.NET MVC Web project is the normal string that I use using ADO.NET connection. So what I did is the following:




                                              • Open the app.config on your EDMX project files.


                                              • Copy its connection string.


                                              • Paste it on the WEB project since this will be used when you start
                                                the application.







                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                I have two projects:



                                                One is for the generated EDMX file and all related models.



                                                The other one is the ASP.NET MVC Web.



                                                I encountered this issue since the connection string that I am using on the ASP.NET MVC Web project is the normal string that I use using ADO.NET connection. So what I did is the following:




                                                • Open the app.config on your EDMX project files.


                                                • Copy its connection string.


                                                • Paste it on the WEB project since this will be used when you start
                                                  the application.







                                                share|improve this answer













                                                I have two projects:



                                                One is for the generated EDMX file and all related models.



                                                The other one is the ASP.NET MVC Web.



                                                I encountered this issue since the connection string that I am using on the ASP.NET MVC Web project is the normal string that I use using ADO.NET connection. So what I did is the following:




                                                • Open the app.config on your EDMX project files.


                                                • Copy its connection string.


                                                • Paste it on the WEB project since this will be used when you start
                                                  the application.








                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jan 18 at 18:47









                                                Willy David JrWilly David Jr

                                                4,06821727




                                                4,06821727























                                                    -2














                                                    Add metadata="res:///MyModel.csdl|res:///MyModel.ssdl|res://*/MyModel.msl" to connectionstring and providerName="System.Data.EntityClient" will resolve the issue






                                                    share|improve this answer



















                                                    • 1





                                                      This question really doesn't need yet another answer stating this.

                                                      – Gert Arnold
                                                      Jul 28 '18 at 13:58
















                                                    -2














                                                    Add metadata="res:///MyModel.csdl|res:///MyModel.ssdl|res://*/MyModel.msl" to connectionstring and providerName="System.Data.EntityClient" will resolve the issue






                                                    share|improve this answer



















                                                    • 1





                                                      This question really doesn't need yet another answer stating this.

                                                      – Gert Arnold
                                                      Jul 28 '18 at 13:58














                                                    -2












                                                    -2








                                                    -2







                                                    Add metadata="res:///MyModel.csdl|res:///MyModel.ssdl|res://*/MyModel.msl" to connectionstring and providerName="System.Data.EntityClient" will resolve the issue






                                                    share|improve this answer













                                                    Add metadata="res:///MyModel.csdl|res:///MyModel.ssdl|res://*/MyModel.msl" to connectionstring and providerName="System.Data.EntityClient" will resolve the issue







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Jul 28 '18 at 13:43









                                                    Abhas BhoiAbhas Bhoi

                                                    153




                                                    153








                                                    • 1





                                                      This question really doesn't need yet another answer stating this.

                                                      – Gert Arnold
                                                      Jul 28 '18 at 13:58














                                                    • 1





                                                      This question really doesn't need yet another answer stating this.

                                                      – Gert Arnold
                                                      Jul 28 '18 at 13:58








                                                    1




                                                    1





                                                    This question really doesn't need yet another answer stating this.

                                                    – Gert Arnold
                                                    Jul 28 '18 at 13:58





                                                    This question really doesn't need yet another answer stating this.

                                                    – Gert Arnold
                                                    Jul 28 '18 at 13:58


















                                                    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%2f37528405%2fentity-framework-the-context-is-being-used-in-code-first-mode-with-code-that-wa%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

                                                    Homophylophilia

                                                    Updating UILabel text programmatically using a function

                                                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage