实体框架6 usetransaction databasefirst throws UnintentionalCodeFirstException [英] Entity framework 6 usetransaction databasefirst throws UnintentionalCodeFirstException

查看:191
本文介绍了实体框架6 usetransaction databasefirst throws UnintentionalCodeFirstException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

  SqlConnection cnSql = new SqlConnection(cnstring); 
cnSql.Open();
SqlTransaction Transactie = default(SqlTransaction);
Transactie = cnSql.BeginTransaction();
SqlCommand cmddelbonh = new SqlCommand();
cmddelbonh.Connection = Transactie.Connection;
cmddelbonh.Transaction = Transactie;
cmddelbonh.CommandText =update artikelgroepen set OMSN ='XXXXX'where ID = 3;
cmddelbonh.ExecuteNonQuery();使用(CXNACC_TESTFIRMA cxn = new CXNACC_TESTFIRMA(cnSql))



{
cxn.Database.UseTransaction(Transactie);
var myList = cxn.ARTIKELS.Take(100).ToList();
cxn.ARTIKELS.First()。OMSN =XXXXX;
cxn.SaveChanges();
}

Transactie.Commit();

此行
cxn.Database.UseTransaction(Transactie);
在上下文中输入OnModelCreating并抛出一个UnintentionalCodeFirstException



当注释掉新的UnintentionalCodeFirstException
它可以工作,但我不喜欢注释掉一个不应该被触发的错误。



我googled了,但没有找到关于如何在数据库第一个上下文中使用EF6的UseTransaction功能的答案。 / p>

在您询问之前:实体的connectionString存在于app.config中。



有任何想法?



谢谢

解决方案

我发现这个文章,它解释了更多关于数据库优先vs代码优先...



似乎使用构造函数将连接作为参数(您的行: using(CXNACC_TESTFIRMA cxn = new CXNACC_TESTFIRMA cnSql)))将我们e代码优先策略来构建上下文



但是,我被告知以下内容:


对于EF使用您的EDMX文件的元数据,您需要传入一个
EntityConnection而不是直接的SqlConnection。



如果你想SqlConnection能够用于外部的其他任务
,那么您可以随时创建EntityConnection
,然后从其中获取StoreConnection:




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




由于EntityConnection源自DbConnection,您仍然可以通过
它变成与您尝试使用的DbContext相同的构造函数。



I'm trying to execute the following :

  SqlConnection cnSql = new SqlConnection(cnstring);
        cnSql.Open();
        SqlTransaction Transactie = default(SqlTransaction);
        Transactie = cnSql.BeginTransaction();
        SqlCommand cmddelbonh = new SqlCommand();
        cmddelbonh.Connection = Transactie.Connection;
        cmddelbonh.Transaction = Transactie;
        cmddelbonh.CommandText = "update artikelgroepen set OMSN='XXXXX' where ID = 3";
        cmddelbonh.ExecuteNonQuery();


        using (CXNACC_TESTFIRMA cxn = new CXNACC_TESTFIRMA(cnSql))
        {
            cxn.Database.UseTransaction(Transactie);
            var myList = cxn.ARTIKELS.Take(100).ToList();
            cxn.ARTIKELS.First().OMSN = "XXXXX";
            cxn.SaveChanges();
        }

        Transactie.Commit();

This line cxn.Database.UseTransaction(Transactie); enters the OnModelCreating on the Context and throws a UnintentionalCodeFirstException

When commenting out the throw new UnintentionalCodeFirstException it works but I don't like having to comment out an error which shouldn't be triggered anyway.

I googled around, but found no answers as to how use the UseTransaction feature of EF6 in a databasefirst context.

Before you ask : the connectionString for Entity is present in the app.config.

Any thoughts on this please?

Thanks

解决方案

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.

这篇关于实体框架6 usetransaction databasefirst throws UnintentionalCodeFirstException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆