Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
fix AutoIncrement issues with SQL Server 2012
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Feb 27, 2018
1 parent 2b7e948 commit 04e2cc5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Expand Up @@ -25,15 +25,17 @@ public override bool DoesSequenceExist(IDbCommand dbCmd, string sequenceName)

protected override string GetAutoIncrementDefinition(FieldDefinition fieldDef)
{
if (fieldDef.AutoIncrement && !string.IsNullOrEmpty(fieldDef.Sequence))
if (!string.IsNullOrEmpty(fieldDef.Sequence))
return $"DEFAULT NEXT VALUE FOR {Sequence(NamingStrategy.GetSchemaName(GetModel(fieldDef.PropertyInfo?.ReflectedType)), fieldDef.Sequence)}";
else
return AutoIncrementDefinition;
}

protected override bool ShouldSkipInsert(FieldDefinition fieldDef) => fieldDef.ShouldSkipInsert() && !fieldDef.AutoIncrement && string.IsNullOrEmpty(fieldDef.Sequence);
protected override bool ShouldSkipInsert(FieldDefinition fieldDef) =>
fieldDef.ShouldSkipInsert() && string.IsNullOrEmpty(fieldDef.Sequence);

protected override bool SupportsSequences(FieldDefinition fieldDef) => fieldDef.AutoIncrement || !string.IsNullOrEmpty(fieldDef.Sequence);
protected override bool SupportsSequences(FieldDefinition fieldDef) =>
!string.IsNullOrEmpty(fieldDef.Sequence);

public override List<string> ToCreateSequenceStatements(Type tableType)
{
Expand Down
Expand Up @@ -313,7 +313,8 @@ protected string Sequence(string schema, string sequence)
protected virtual bool ShouldReturnOnInsert(ModelDefinition modelDef, FieldDefinition fieldDef) =>
fieldDef.ReturnOnInsert || (fieldDef.IsPrimaryKey && fieldDef.AutoIncrement && modelDef.HasReturnAttribute);

protected virtual bool ShouldSkipInsert(FieldDefinition fieldDef) => fieldDef.ShouldSkipInsert();
protected virtual bool ShouldSkipInsert(FieldDefinition fieldDef) =>
fieldDef.ShouldSkipInsert();

protected virtual bool SupportsSequences(FieldDefinition fieldDef) => false;

Expand Down
26 changes: 12 additions & 14 deletions src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs
Expand Up @@ -645,33 +645,31 @@ internal static long Insert<T>(this IDbCommand dbCmd, T obj, Action<IDbCommand>
if (modelDef.HasReturnAttribute)
{
using (var reader = dbCmd.ExecReader(dbCmd.CommandText))
using (reader)
{
using (reader)
if (reader.Read())
{
if (reader.Read())
var values = new object[reader.FieldCount];
var indexCache = reader.GetIndexFieldsCache(ModelDefinition<T>.Definition, dialectProvider);
obj.PopulateWithSqlReader(dialectProvider, reader, indexCache, values);
if ((modelDef.PrimaryKey != null) && modelDef.PrimaryKey.AutoIncrement)
{
var values = new object[reader.FieldCount];
var indexCache = reader.GetIndexFieldsCache(ModelDefinition<T>.Definition, dialectProvider);
obj.PopulateWithSqlReader(dialectProvider, reader, indexCache, values);
if ((modelDef.PrimaryKey != null) && modelDef.PrimaryKey.AutoIncrement)
{
var id = modelDef.GetPrimaryKey(obj);
return Convert.ToInt64(id);
}
var id = modelDef.GetPrimaryKey(obj);
return Convert.ToInt64(id);
}
return 0;
}
return 0;
}
}
else

if (selectIdentity)
{
dbCmd.CommandText += dialectProvider.GetLastInsertIdSqlSuffix<T>();

return dbCmd.ExecLongScalar();
}
else
return dbCmd.ExecNonQuery();

return dbCmd.ExecNonQuery();
}

internal static void Insert<T>(this IDbCommand dbCmd, Action<IDbCommand> commandFilter, params T[] objs)
Expand Down

0 comments on commit 04e2cc5

Please sign in to comment.