C#Web Api中的连接字符串错误 [英] Connection String errors in C# Web Api

查看:127
本文介绍了C#Web Api中的连接字符串错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#.Net中创建一个Web Api。我已经使用Unity.Mvc5和 Db First 方法在其中实现了依赖注入。从那时起,我在连接字符串中遇到了一些问题。其中有一个默认的 AccountController ,我已经创建了一个 TestController 来测试我的Api。
我的Web.config文件中有两个连接字符串(其中一个已注释,但仅在此处显示,我对此未加注释)。

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" />

现在的问题是,当我使用第一个字符串时,我的测试控制器Api无法正常工作。给出的例外是:

Now the problem is when I use the first string my test controller Api's don't work. The exception given is:


上下文在代码优先模式下使用,该上下文是从EDMX文件为数据库优先或数据库生成的。模型优先开发。这将无法正常工作。若要解决此问题,请不要删除引发此异常的代码行。如果希望使用数据库优先或模型优先,请确保在启动项目的app.config或web.config中包含Entity Framework连接字符串。如果要创建自己的DbConnection,请确保它是EntityConnection而不是其他类型的DbConnection,并将其传递给采用DbConnection的基本DbContext构造函数之一。要了解有关代码优先,数据库优先和模型优先的更多信息,请参见此处的实体框架文档: http ://go.microsoft.com/fwlink/?LinkId = 394715

当我使用第二个连接字符串时,我的帐户控制器无法正常工作并产生异常:

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


尝试创建类型为 AccountController的控制器时发生错误。确保控制器具有无参数的公共构造函数。

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

我的UnityConfig.cs如下:

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.

预先感谢。

编辑+解决方案:
根据Roman的回答,我将第一个连接字符串命名为DefaultConnection,在ApplicationDbContext类构造函数中,我给了这个DefaultConnection。现在,我的AccountController使用此连接字符串,而所有其他控制器使用第二个连接字符串。
希望对您有所帮助。

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.

推荐答案

那是因为AccountController在CodeFirst模式下使用了DbContext。我认为最好使用单独的DbContext:第一个仅用于身份验证和授权(在CodeFirst模式下),第二个用于其他东西(在DatabaseFirst模式下)。

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).

可以尝试将AccountController配置为在DatabaseFirst模式下使用DbContext。

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

这篇关于C#Web Api中的连接字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆