Subsonic 3 简单存储库和交易

发布于 2024-07-26 16:49:14 字数 1101 浏览 5 评论 0原文

这就是我到目前为止所拥有的。 是我做错了什么还是 3.0.0.3 中有错误?

    var Repository = new SimpleRepository("DBConnectionName");

    using (TransactionScope ts = new TransactionScope())
    {
        using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName"))
        {
            try
            {
                for (int i = 0; i < 5; i++)
                {
                    Supplier s = new Supplier();
                    s.SupplierCode = i.ToString();
                    s.SupplierName = i.ToString();

                    Repository.Add<Supplier>(s);
                }

                ts.Complete();
            }
            catch
            {
            }
        }
    }

我在 SubSonic DbDataProvider 中遇到错误 公共 DbConnection CurrentSharedConnection { 获取{返回__sharedConnection; }

        protected set
        {
            if(value == null)
            {
                __sharedConnection.Dispose();

ETC.. __sharedConnection == null :( 对象空引用异常 :(

So this is what I have so far. Am I doing something wrong or is there a bug in 3.0.0.3?

    var Repository = new SimpleRepository("DBConnectionName");

    using (TransactionScope ts = new TransactionScope())
    {
        using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName"))
        {
            try
            {
                for (int i = 0; i < 5; i++)
                {
                    Supplier s = new Supplier();
                    s.SupplierCode = i.ToString();
                    s.SupplierName = i.ToString();

                    Repository.Add<Supplier>(s);
                }

                ts.Complete();
            }
            catch
            {
            }
        }
    }

I'm getting an error in SubSonic DbDataProvider
public DbConnection CurrentSharedConnection
{
get { return __sharedConnection; }

        protected set
        {
            if(value == null)
            {
                __sharedConnection.Dispose();

etc..
__sharedConnection == null :( Object Null Reference Exception :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见终念 2024-08-02 16:49:14

终于自己解决了这个问题。 上述所有代码对我来说都不起作用(SubSonic 3.0.0.3,使用 SQLite),但添加 BeginTransaction() 使其按预期工作,大大加快了事务速度并回滚更新(如果有)出现异常情况。

using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope(Access.Provider))
{
    using (IDbTransaction ts = sharedConnectionScope.CurrentConnection.BeginTransaction())
    {
        IRepository repo = new SimpleRepository(Access.Provider);
        //Do your database updates

        //throw new ApplicationException("Uncomment this and see if the updates get rolled back");
        ts.Commit();
    }
}

为了完整起见:Access.Provider 是我的辅助类中的一个静态属性,它返回 return SubSonic.DataProviders.ProviderFactory.GetProvider(ConnectionString, "System.Data.SQLite");

Finally solved this for myself. All of the above code does not work for me (SubSonic 3.0.0.3, using SQLite) but adding BeginTransaction() caused it to work as expected, greatly speeding up the transaction and rolling back the updates if any exceptions occur.

using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope(Access.Provider))
{
    using (IDbTransaction ts = sharedConnectionScope.CurrentConnection.BeginTransaction())
    {
        IRepository repo = new SimpleRepository(Access.Provider);
        //Do your database updates

        //throw new ApplicationException("Uncomment this and see if the updates get rolled back");
        ts.Commit();
    }
}

For completeness: Access.Provider is a static property in a helper class for me that returns return SubSonic.DataProviders.ProviderFactory.GetProvider(ConnectionString, "System.Data.SQLite");

一绘本一梦想 2024-08-02 16:49:14

也许切换 SharedDbConnectionScope 和 TransactionScope 可能会有所帮助。

using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName"))
{
    using (TransactionScope ts = new TransactionScope())
    {
    }
}

Perhaps switching the SharedDbConnectionScope and TransactionScope around may help.

using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName"))
{
    using (TransactionScope ts = new TransactionScope())
    {
    }
}
木緿 2024-08-02 16:49:14

当设置迁移时会发生这种情况 - 在表迁移时,数据库连接将关闭。

尝试使用 SimpleRepositoryOptions.None 的 SimpleRepository。

不知道这是否是一个错误。 我认为事务不适用于 SimpleRepository,在事务中抛出异常时我总是保存一半的数据......也许它只适用于 ActiveRecord? 有人知道吗?

This will happen when Migration is set - On tablemigration the dbconnection will be closed.

Try the SimpleRepository with SimpleRepositoryOptions.None.

Don't know if this is a bug. I think the transactions don't work with SimpleRepository, I've always half of the data saved when throwing an exception in the transaction... perhaps it's only for ActiveRecord? Anybody knows?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文