Jaa


在WebSites中使用EntityFramework访问SQL Database时可能遇到的一个连接问题

 

???????????,????????Microsoft Azure???????:??Azure WebSites?????Web??,????SQL Database????????????;??Entity Framework,??????????????????,????SQL Database??????????

???:??????,???Visual Studio???????WebSites??,???Azure??SQL Database???,????;?????????Azure??WebSites???,??SQL Database???????

???????????:

Server Error in '/' Application.

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

???????????????Azure WebSites????????????

??????,?????Visual Studio???????????SQL Database????????????,????“ADO.NET Entity Data Model”,????:

clip_image001

??EF Designer from database:

clip_image002

???,?????????(Connection String)??:

clip_image003

??,?????SQL Database?????????????????:

clip_image004

OK,???????

??????,?app.config???,???????,??:

<connectionStrings>

<add name="DefaultConnection" connectionString="Data Source=tcp:[SERVERNAME].database.chinacloudapi.cn,1433;initial catalog=[CATNAME]; persist security info=True; user id=[USERID];password=[PASSWORD];" providerName="System.Data.SqlClient" />

<add name="[ENTITYNAME]" connectionString="metadata=res://*/ [MODEL].csdl|res://*/ [MODEL].ssdl|res://*/ [MODEL].msl; provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:[SERVERNAME].database.chinacloudapi.cn,1433;initial catalog=[CATNAME]; persist security info=True; user id=[USERID];password=[PASSWORD];multipleactiveresultsets=True;App=EntityFramework&quot;"

providerName="System.Data.EntityClient"/>

</connectionStrings>

?????????,??????????

??????Azure Websites??????,??Web Site????SQL Database??,??:

clip_image005

???,??????????“DefaultConnection”????????????

??????(Management Portal)??????,???:

clip_image006

????Web Site????,??,???,????“DefaultConnection”?????

clip_image008

??????,?WebSites?????????(??“DefaultConnection”),???Visual Studio???????????????????????,????????????????

??????Azure WebSites??,Entity Framework?????????,??WebSites?????????,???EntityFramework??????????

?????????????Entity Framework?????,????????????

??Entity Framework,???GitHub???????????????:

??:https://github.com/aspnet/EntityFramework

Scott Gu?????EntityFramework???????:database first,model first?code first???????

??: https://weblogs.asp.net/scottgu/code-first-development-with-entity-framework-4

???WebSites?Azure???????,???Visual Studio??????????????????????,????????????????????,??????????