Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

ef core set identity_insert off

#region IDENTITY_INSERT

        public static void EnableIdentityInsert<T>(this DbContext context) => SetIdentityInsert<T>(context, true);
        public static void DisableIdentityInsert<T>(this DbContext context) => SetIdentityInsert<T>(context, false);

        private static void SetIdentityInsert<T>([NotNull] DbContext context, bool enable)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            var entityType = context.Model.FindEntityType(typeof(T));
            var value = enable ? "ON" : "OFF";
            context.Database.ExecuteSqlRaw($"SET IDENTITY_INSERT {entityType.GetSchema()}.{entityType.GetTableName()} {value}");
        }

        public static void SaveChangesWithIdentityInsert<T>([NotNull] this DbContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            using var transaction = context.Database.BeginTransaction();
            context.EnableIdentityInsert<T>();
            context.SaveChanges();
            context.DisableIdentityInsert<T>();
            transaction.Commit();
        }

        #endregion 

        #region IDENTITY_INSERT ASYNC

        public static async Task EnableIdentityInsertAsync<T>(this DbContext context) => await SetIdentityInsertAsync<T>(context, true);
        public static async Task DisableIdentityInsertAsync<T>(this DbContext context) => await SetIdentityInsertAsync<T>(context, false);

        private static async Task SetIdentityInsertAsync<T>([NotNull] DbContext context, bool enable)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            var entityType = context.Model.FindEntityType(typeof(T));
            var value = enable ? "ON" : "OFF";
            await context.Database.ExecuteSqlRawAsync($"SET IDENTITY_INSERT {entityType.GetSchema()}.{entityType.GetTableName()} {value}");
        }

        public static async Task SaveChangesWithIdentityInsertAsync<T>([NotNull] this DbContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            await using var transaction = await context.Database.BeginTransactionAsync();
            await context.EnableIdentityInsertAsync<T>();
            await context.SaveChangesAsync();
            await context.EnableIdentityInsertAsync<T>();
            await transaction.CommitAsync();
        }


        #endregion 
Comment

PREVIOUS NEXT
Code Example
Csharp :: git find commits by file path 
Csharp :: regex c# 
Csharp :: c# last char in string 
Csharp :: c# double to int 
Csharp :: difference between class and struct in c# 
Csharp :: c# razor add disabled to button if 
Csharp :: raylib c# 
Csharp :: streamwriter c# 
Csharp :: billboard canvas unity 
Csharp :: string to camel case c# 
Csharp :: unity editor script 
Csharp :: c# convert datetime to unix timestamp 
Csharp :: 1 line if c# 
Csharp :: Convert DataTable to Dictionary in C# 
Csharp :: get property value from object c# 
Csharp :: c# dictionary keys to list 
Csharp :: json property c# 
Csharp :: c# get total milliseconds from datetime 
Csharp :: on collision enter by layer 2d unity 
Csharp :: multiplication using arrays 
Csharp :: asp.net core miniprofiler 
Csharp :: Dyanmically create datatable in c# 
Csharp :: how do you make a 2D object follow another 2D object in unity 2D 
Csharp :: C# setting property values through reflection with attributes 
Csharp :: c# add time to datetime 
Csharp :: dotween sequence 
Csharp :: cs string to enum 
Csharp :: string tochararray c# 
Csharp :: c# debug writeline 
Csharp :: .net 6 autofac 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =