背景:
之前项目使用的是DB first/Model first,现在要对EF升级的6.0,并且更换成Code first。
问题:
1. System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource.
<add name="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.
<add name="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.
<add name="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程序里的写法一样;
<add name="TestContext" connectionString="Data Source=[***DB Server Name];Initial Catalog=[***DB Name***];Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Reference link: