.NET開發中基礎問題,CODE First AND DB First(大牛自動忽略,小白可以看一下)

這個宅男喜歡蘿莉發表於2018-10-22

最近在做一個新專案開發時,碰到了下面這個問題.在使用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” />

相關文章