MVC: Connection String

 

背景:

之前项目使用的是DB first/Model first,现在要对EF升级的6.0,并且更换成Code first。

 

问题:

1. System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource.

"TestContext" connectionString="metadata=res://*/Test.csdl|res://*/Test.ssdl| res://*/Test.msl;     provider=System.Data.SqlClient;provider connection string="Data Source=tcp:[***test***].database.windows.net,1433;Initial Catalog=test;persist security info=True;User ID=[***test***];Password=[***test***];multipleactiveresultsets=True;Encrypt=True;Trusted_Connection=false;App=EntityFramework;Connection Timeout=30;"" providerName="System.Data.EntityClient"/>


2. 更改metadata值;

System.ArgumentException: Argument 'xmlReader' is not valid. A minimum of one .ssdl artifact must be supplied.

"TestContext" connectionString="metadata=res://*/;     provider=System.Data.SqlClient;provider connection string="Data Source=tcp:[***test***].database.windows.net,1433;Initial Catalog=test;persist security info=True;User ID=[***test***];Password=[***test***];multipleactiveresultsets=True;Encrypt=True;Trusted_Connection=false;App=EntityFramework;Connection Timeout=30;"" providerName="System.Data.EntityClient"/>

 

3. 删除metadata;

System.ArgumentException: Some required information is missing from the connection string. The 'metadata' keyword is always required.

"TestContext" connectionString="    provider=System.Data.SqlClient;provider connection string="Data Source=tcp:[***test***].database.windows.net,1433;Initial Catalog=test;persist security info=True;User ID=[***test***];Password=[***test***];multipleactiveresultsets=True;Encrypt=True;Trusted_Connection=false;App=EntityFramework;Connection Timeout=30;"" providerName="System.Data.EntityClient"/>

 

解决方法:

对于code first, connectionstring 跟之前的web form程序里的写法一样;

    "TestContext" connectionString="Data Source=[***DB Server Name];Initial Catalog=[***DB Name***];Integrated Security=True;MultipleActiveResultSets=True"  providerName="System.Data.SqlClient" />

 

 

Reference link:

http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx?Redirected=true

转载于:https://www.cnblogs.com/LeimOO/p/3637496.html

你可能感兴趣的:(MVC: Connection String)