EntityFramework database configuration (code frist), entityframework
Do not 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 Source=.;Integrated Security=True" />--> </parameters> </defaultConnectionFactory> </entityFramework></configuration>
The above is a problem with my configuration. I used the default database connection test.
In EF, I roughly saw three database connection Factory types. I used LocalDbConnectionFactory and SqlConnectionFactory. These two types have some differences in configuration, and the database name cannot be specified by default.
<?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"/> <parameter value="Data Source=.;Initial Catalog=EntityContext;User ID=sa; Password=123456;MultipleActiveResultSets=True" /> </parameters> </defaultConnectionFactory>--> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> <parameters> <!--<parameter value="v11.0"/>--> <parameter value="Data Source=.;Initial Catalog=EntityContext;User ID=sa; Password=123456;MultipleActiveResultSets=True" /> </parameters> </defaultConnectionFactory> </entityFramework> <connectionStrings> <add name="EntityContext" connectionString="Data Source=.; Database=EntityContext; User ID=sa; Password=123456;" providerName="System.Data.SqlClient"/> </connectionStrings></configuration>
After repeated tests, the database connection information of Model frist and DB frist is placed in the connectionStrings label, and the code frist is placed in the parameter of entityFramework, and the connection string needs to be added
MultipleActiveResultSets = True)
In addition, the System. Data. Entity. Infrastructure. SqlConnectionFactory database factory class is replaced,
If MSSQL is used, use the Connection Library SqlConnectionFactory and the name of the database named by yourself, and write the link configuration under the <connectionStrings> tab. The connection name is determined in the code.
Public EntityContext (): base ("name = EntityContext") // This is not the database name, it is the name of the link string {}