.NET开发中基础问题,CODE First AND DB First(大牛自动忽略,小白可以看一下)

最近在做一个新项目开发时,碰到了下面这个问题.在使用EF时,提示错误信息

To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

在网上也找了一些回复,但是基本都没有说明怎么解决的,我在这里说一下,

这个原因是因为WEB.Config中配置的连接字符串决定的.不同的连接字符串是不一样的

下面这种字符串是CODE First的,

<add name="XXXX" providerName="System.Data.SqlClient" connectionString="Data Source=XX.XX.X8.XX;Initial Catalog=XX;Persist Security Info=True;User ID=XX;Password=XX;"/>

通过EF同步数据库的类,应该使用下面这种链接字符串

<add name="XX" connectionString="metadata=res://*/XX.csdl|res://*/XX.ssdl|res://*/XX.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=XX.XX.XX.XX;initial catalog=XX;persist security info=True;user id=sa;password=XX;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

猜你喜欢

转载自www.cnblogs.com/iloveloli/p/9830638.html