I'm not saying anything. Post Code First
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <!--for more Information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/? linkid=237468-- <section name= "EntityFramework" type= " System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=4.4.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirepermission=" false "/> </configSections> < entityframework> <defaultconnectionfactory type= " System.Data.Entity.Infrastructure.LocalDbConnectionFactory, entityframework "> <parameters> <parameter value= "v11.0"/> </parameters> </defaultConnectionFactory> </ Entityframework></configuration>
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <!--for more information On Entity Framework configuration, visit http://go.microsoft.com/fwlink/? linkid=237468--<section name= "EntityFramework" type= " System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=4.4.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirepermission=" false "/> </configSections> <connectionStrings> <add name= "entitycontextentities" connectionstring= "metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/ Model1.msl;provider=system.data.sqlclient;provider connection string= "Data source=.; Initial catalog=entitycontext;integrated security=true; Multipleactiveresultsets=true; App=entityframework "" Providername= "System.Data.EntityClient"/> </connectionStrings> <entityframework > <defaultconnectionfactory type= "System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework "> <parameters> <parameter value=" v11.0 "/> <!--<parameter value=" Data so Urce=.;i ntegrated security=true "/>--> </parameters> </defaultConnectionFactory> </ENTITYFRAMEWORK&G T;</configuration>
The above is my configuration is the problem, I was using the default database connection test
In EF I saw roughly three kinds of database connection factory, I used localdbconnectionfactory and sqlconnectionfactory, these two have some differences in configuration, and the default configuration cannot specify the database name
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <!--for more information On Entity Framework configuration, visit http://go.microsoft.com/fwlink/? linkid=237468--<section name= "EntityFramework" type= " System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=4.4.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirepermission=" false "/> </configSections> <entityFramework> & lt;! --<defaultconnectionfactory type= "System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework "> <parameters> <parameter value=" v11.0 "/> <parameter value=" Data source=.;i Nitial Catalog=entitycontext; User Id=sa; password=123456; Multipleactiveresultsets=true "/> </parameters> </defaultConnectionFactory>--> <defaultcon Nectionfactory type= "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" > <parameters> <!--<parameter value= "v11.0"/>--> <parameter value= "Data source=.;i Nitial Catalog=entitycontext; User Id=sa; password=123456; Multipleactiveresultsets=true "/> </parameters> </defaultConnectionFactory> </ENTITYFRAMEWORK&G T <connectionStrings> <add name= "Entitycontext" connectionstring= "Data source=.; Database=entitycontext; User Id=sa; password=123456, "providername=" System.Data.SqlClient "/> </connectionStrings></configuration>
After repeated testing, (the Model Frist and DB Frist database connection information is placed in the connectionstrings tag, code frist is placed in EntityFramework parameter, and the connection string needs to be added
Multipleactiveresultsets=true)
There is the replacement of the System.Data.Entity.Infrastructure.SqlConnectionFactory database factory class,
If using MSSQL, to use sqlconnectionfactory this link library and own name of the database name, and under the <connectionStrings> tag to write the link configuration, the code to determine the name of the connection.
Public Entitycontext () : Base ("Name=entitycontext")//This is not a database name, is the name of the link string { }
EntityFramework Database Configuration (Code frist)