Comments (5)
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.
Considering this. If anyone has a good idea on how to best handle this please comment.
from dapper.simplecrud.
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.
With the latest version, SQL Server uses scope_identity, and SQLCE uses @@Identity which should solve this problem.
from dapper.simplecrud.
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)
- SimpleCrud works using oracle driver but not odbc
- Insert with Guid as Key: Inconsistent behaviour async/sync
- When inserting entity with int? Id assigned a value other than null, the assigned value is returned instead of the inserted entity's Id HOT 1
- Add fluent config in separate class
- Type Handler not being called for SimpleCRUD extensions HOT 2
- Support mordern sqlserver page query like OFFSET ROWS FETCH NEXT
- Does the library only support SQL Server and not Oracle?
- Attribute for selective inclusion of columns in insert/update HOT 1
- How to insert in CLOB columns with SimpleCRUD ?
- _getIdentitySql for postgres misaligns with actual id of inserted entity
- Tons of exceptions thrown in trying to generate CRUD, PostgreSQL DB
- Using Guids without Guid type in the database (MariaDB)
- GetListPagedAsync fails on table with HierarchyID field type
- System.IndexOutOfRangeException Index was outside the bounds of the array.
- Complex fields not included in SELECT/UPDATE queries
- Question: A primary key that is not identity. HOT 1
- Add support for DateOnly and TimeOnly
- Ignore Update if property value is null
- Support for composite keys ? HOT 1
- it is a bug about string whereCondition HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dapper.simplecrud.