Entity framework 6 usetransaction databasefirst throws UnintentionalCodeFirstException

时间秒杀一切 提交于 2019-12-06 15:31:56

I found this article which explains a bit more about database-first vs code-first...

It seems that using the constructor taking the connection as parameter (your line: using (CXNACC_TESTFIRMA cxn = new CXNACC_TESTFIRMA(cnSql)) ) will use code-first strategy to build the context

But then I was advised the following:

For EF to use the metadata from your EDMX file you need to pass in an EntityConnection rather than a straight up SqlConnection.

If you want the SqlConnection to be able to use for other tasks outside of the context then you can always create the EntityConnection and then grab the StoreConnection off it:

  var entityConnection = new EntityConnection("name=AdventureWorksEntities");
  var dbConnection = entityConnection.StoreConnection; 

Because EntityConnection derives from DbConnection, you can still pass it into the same constructor on DbContext that you were trying to use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!