Rumah > Artikel > pangkalan data > EF CodeFirst生成数据库到Sqlserver中
EF CodeFirst简单实例 这篇文章介绍了如何用EF去快速生成数据库。但是这个并没有生成到sqlserver中,总觉得不爽。下面就来讲一下,如何将数据库生成到sqlserver中。 按照EF CodeFirst简单实例中介绍的一步一步走,只是最后呢,需要在配置文件中加入connectio
EF CodeFirst简单实例
这篇文章介绍了如何用EF去快速生成数据库。但是这个并没有生成到sqlserver中,总觉得不爽。下面就来讲一下,如何将数据库生成到sqlserver中。
按照EF CodeFirst简单实例中介绍的一步一步走,只是最后呢,需要在配置文件中加入connectionStrings的配置节即可。
配置文件如下:
<span></span><span>xml version="1.0" encoding="utf-8"</span><span>?></span> <span><span>configuration</span><span>></span> <span><span>configdivs</span><span>></span> <span><!--</span><span> For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 </span><span>--></span> <span><span>div </span><span>name</span><span>="entityFramework"</span><span> type</span><span>="System.Data.Entity.Internal.ConfigFile.EntityFrameworkdiv, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"</span><span> requirePermission</span><span>="false"</span> <span>/></span> <span></span><span>configdivs</span><span>></span> <span><span>startup</span><span>></span> <span><span>supportedRuntime </span><span>version</span><span>="v4.0"</span><span> sku</span><span>=".NETFramework,Version=v4.5"</span> <span>/></span> <span></span><span>startup</span><span>></span> <span><span>entityFramework</span><span>></span> <span><span>defaultConnectionFactory </span><span>type</span><span>="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"</span> <span>/></span> <span><span>providers</span><span>></span> <span><span>provider </span><span>invariantName</span><span>="System.Data.SqlClient"</span><span> type</span><span>="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"</span> <span>/></span> <span></span><span>providers</span><span>></span> <span></span><span>entityFramework</span><span>></span> <span><span>connectionStrings</span><span>></span> <span><!--</span><span>Data Source=.;Initial Catalog=GuestBook;Integrated Security=True</span><span>--></span> <span><span>add </span><span>name</span><span>="WeiXinDB1"</span><span> connectionString</span><span>="Data Source=.;Initial Catalog= MyDb22;Integrated Security=True"</span><span> providerName</span><span>="System.Data.SqlClient"</span> <span>/></span> <span></span><span>connectionStrings</span><span>></span> <span></span><span>configuration</span><span>></span></span></span></span></span></span></span></span></span></span></span></span>
下面是说明:
注意:如果你用EF CodeFirst简单实例生成好了之后,并且已经生成过数据库了,那么你再加connectionStrings配置节就没有效果了(不能生成数据库到Sqlserver中)。在这种情况下,需要做如下事情才能将数据库加入数据库中:
先删除引用中的entityFramework,与EntityFramework.SqlServer。然后再重新引用(安装了EntityFramework之后就会有这两个dll)。之后编译即可。