标签:
什么也不说先贴代码
<?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>
以上是我在配置是出的问题,我当时采用的是默认数据库连接测试
在EF中我大致看到了三种数据库连接的Factory,我用到了LocalDbConnectionFactory和SqlConnectionFactory,这两个在配置上有一定的差异,而且默认配置不能指定数据库名
<?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>
经过反复测试,(Model frist和DB frist的数据库连接信息是放在connectionStrings标签内,code frist则放在entityFramework的parameter中,且连接字符串需要加上
MultipleActiveResultSets=True)
还有就是更换System.Data.Entity.Infrastructure.SqlConnectionFactory数据库工厂类,
如果使用MSSQL,要用SqlConnectionFactory这个链接库和自己命名的数据库名,并在<connectionStrings>标签下写链接配置,代码中确定连接的名字。
public EntityContext() : base("name=EntityContext")//此处不是数据库名,是链接字符串的名字 { }
EntityFramework数据库配置(code frist)
标签:
原文地址:http://www.cnblogs.com/RainbowInTheSky/p/4461053.html