Connection string errors in C # Web Api

I am building a web api in C # .Net. I used Injection Dependency in it using Unity.Mvc5 and using the Db First approach. Since then, I have encountered some problems in connection chains. It uses the AccountController by default, and I created a TestController to test my Api. There are two connection lines in my Web.config file (one of them is commented out, but just to show it here I uncommented it).

<add name="DDEXEntities" connectionString="Data Source=DESKTOP-CSB6551;initial catalog=DDEX;integrated security=True" providerName="System.Data.SqlClient" /> <add name="DDEXEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-CSB6551;initial catalog=DDEX;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 

Now the problem is that when I use the first line, my Api test controller does not work. The exception is:

The context is used in Code First mode with the code that was generated from the EDMX file for developing Database First or Model First. This will not work correctly. To resolve this issue, do not delete the line of code that throws this exception. If you want to use Database First or Model First, make sure that the Entity Framework connection string is included in the app.config or web.config of the startup project. If you create 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 basic DbContext constructors that accept 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

And when I use the second connection string, my account controller is down and throws an exception:

An error occurred while trying to create a controller of type AccountController. Make sure that the controller does not have a constructor without parameters.

My UnityConfig.cs is as follows:

 public static class UnityConfig { public static void RegisterComponents() { var container = new UnityContainer(); container.RegisterType<DbContext, DDEXEntities>(new PerResolveLifetimeManager()); // register all your components with the container here // it is NOT necessary to register your controllers container.RegisterType<AccountController>(new InjectionConstructor()); // eg container.RegisterType<ITestService, TestService>(); container.RegisterType<IGenreService, GenreService>(new TransientLifetimeManager()); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); } } 

Can someone tell me what I did wrong. Now it's nervous.

Thanks in advance.

Edit + Solution: Based on the Roman answer, I named my first connection string as DefaultConnection, and in the constructor of the ApplicationDbContext class I gave it DefaultConnection. Now my AccountController uses this connection string, and all other controllers use the second connection string. Hope this helps someone.

0
source share
1 answer

This is because AccountController uses your DbContext in CodeFirst mode. I think it's better to use a separate DbContext: first for authentication and authorization (in CodeFirst mode), and the second for other things (in DatabaseFirst mode).

You can also try configuring the AccountController to use DbContext in DatabaseFirst mode.

+2
source

All Articles