Connection String errors in C# Web Api

2019-08-25 14:16发布

I am creating a Web Api in C#.Net. I have implemented Dependency Injection in it using Unity.Mvc5 and using Db First approach. Since then i'm facing some issues in connection strings. There is a default AccountController in it and I have created a TestController to test my Api's. There are two connection strings in my Web.config file (one of them is commented but just to show here i have 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 when I use the first string my test controller Api's don't work. The exception given is:

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

And when I use the second connection string, my Account controller doesn't work and produces exception:

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

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());

        // e.g. container.RegisterType<ITestService, TestService>();
        container.RegisterType<IGenreService, GenreService>(new TransientLifetimeManager());

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
    }
}

Can anyone let me know what I have done wrong. This is getting on my nerves now.

Thanks in advance.

Edit + Solution: Based on Roman's answer, 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. I hope it helps someone.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-25 14:31

That's because AccountController uses your DbContext in CodeFirst mode. I think it's better to use separate DbContext: first one just for authentication and authorization(in CodeFirst mode) and second one for other stuff(in DatabaseFirst mode).

Also you can try to configure AccountController to use DbContext in DatabaseFirst mode.

查看更多
登录 后发表回答