开发者

Unit tests connectionstring error

开发者 https://www.devze.com 2023-02-22 21:14 出处:网络
I am working in MVC application. I have this in my context class: public ProductsContext() : base(\"ProductyDB\")

I am working in MVC application. I have this in my context class:

    public ProductsContext()
        : base("ProductyDB")
    {
        Database.SetInitializer<ProductsContext>(null);
    }

my web.config connection string looks like this:

<add 
      name="ProductyDB"
      providerName="System.Data.SqlClient"
      connectionString="Data Source=.\SQLSERVER2开发者_StackOverflow社区008R2;Initial Catalog=ProductyDB;Integrated Security=True"/>

it works fine in MVC applicationbut as soon as I used the entities and function in a new Unit test project added to this solution from this MVC application, I get errors like tblProduct not available.

Temporary workaround: If I comment the following lines from my ProductsContext:

public ProductsContext() : base("ProductyDB") { Database.SetInitializer(null); }

and change my web.config connection string name to ProductsContext, It works.


I had an issue similar to this one and it turns out I had an outdated/incorrect table name set in the DbContext class declaration. Probably a unique mistake, but my code was working properly for everything except the unit tests.

public ProductsContext()
    : base("ProductyDB") //This pointed at the wrong database name
{
    Database.SetInitializer<ProductsContext>(null);
}

I thought it was the ConnectionString name that you were supposed to put in, but it really was the Initial Catalog you were passing in.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号