GithubHelp home page GithubHelp logo

Comments (5)

chrishylton avatar chrishylton commented on May 14, 2024

I tried one solution, seem to work well but definitely a kludge just using a try/catch (ick) where it tries using scope_identity() first, and if a parsing error occurs then it retries with @@identity.

    public static int Insert(this IDbConnection connection, object entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        var name = GetTableName(entityToInsert);

        var sb = new StringBuilder();
        sb.AppendFormat("insert into [{0}]", name);
        sb.Append(" (");
        BuildInsertParameters(entityToInsert, sb);
        sb.Append(") values (");
        BuildInsertValues(entityToInsert, sb);
        sb.Append("); select cast(scope_identity() as int)");

        if (Debugger.IsAttached)
            Trace.WriteLine(String.Format("Insert: {0}", sb));

        try
        {
            var newId = connection.Query<int?>(sb.ToString(), entityToInsert).Single();
            return (newId == null) ? 0 : (int)newId;               
        }
        catch (Exception e)
        {
            if (e.Message.ToLower().StartsWith("there was an error parsing"))
            {
                sb = new StringBuilder();
                sb.AppendFormat("insert into [{0}]", name);
                sb.Append(" (");
                BuildInsertParameters(entityToInsert, sb);
                sb.Append(") values (");
                BuildInsertValues(entityToInsert, sb);
                sb.Append(");");

                if (Debugger.IsAttached)
                    Trace.WriteLine(String.Format("Insert: {0}", sb));

                connection.Execute(sb.ToString(), entityToInsert, transaction, commandTimeout);
                var r = connection.Query("select @@identity id", null, transaction, true, commandTimeout);
                return (int)r.First().id;
            }
            else
            {
                throw e;
            }
        }
    }

from dapper.simplecrud.

ericdc1 avatar ericdc1 commented on May 14, 2024

Considering this. If anyone has a good idea on how to best handle this please comment.

from dapper.simplecrud.

chrishylton avatar chrishylton commented on May 14, 2024

Cool. It'd be nice to have something returning the appropriate identity. By 'considering this', hope you don't mean my goofy solution above. It's not a solution, just something to exhibit the problem and was just a quick cut-n-paste to get something working. I ultimately decided NOT to maintain a custom SimpleCRUD project and I ended up dealing with it manually, going back to the DB on inserts using a unique index to get the identity/pk on the record. The error "there was an error parsing" could come back for a whole pile of reasons. Has to be a better way, and avoid my redundancy in the code above. Ugh. I'll give this more thought...

from dapper.simplecrud.

ericdc1 avatar ericdc1 commented on May 14, 2024

With the latest version, SQL Server uses scope_identity, and SQLCE uses @@Identity which should solve this problem.

from dapper.simplecrud.

chrishylton avatar chrishylton commented on May 14, 2024

Thanks for the update to the code, will definitely make use of this feature by removing additional trips back to the DB now. Excellent !

from dapper.simplecrud.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.