به روز رسانی تمام فیلدهای رشتهای تمام جداول بانک اطلاعاتی توسط Entity framework 6.x
نویسنده: وحید نصیری
تاریخ: ۱۳۹۷/۰۲/۲۹ ۹:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using EFReplaceAll.Models;
namespace EFReplaceAll.Config
{
public class DbSetInfo
{
public IQueryable<object> DbSet { set; get; }
public Type DbSetType { set; get; }
}
public class MyContext : DbContext
{
public DbSet<Product> Products { set; get; }
public DbSet<Category> Categories { set; get; }
public DbSet<User> Users { set; get; }
public MyContext()
: base("Connection1")
{
this.Database.Log = sql => Console.Write(sql);
}
public IList<DbSetInfo> GetAllDbSets()
{
return this.GetType()
.GetProperties()
.Where(p => p.PropertyType.IsGenericType &&
p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>))
.Select(p => new DbSetInfo
{
DbSet = (IQueryable<object>)p.GetValue(this, null),
DbSetType = p.PropertyType.GetGenericArguments().First()
})
.ToList();
}
}
} using System;
namespace EFReplaceAll.Utils
{
public class ReplaceOp
{
public string ToFind { set; get; }
public string ToReplace { set; get; }
public StringComparison Comparison { set; get; }
}
} using System.Collections.Generic;
using System.Linq;
using EFReplaceAll.Config;
namespace EFReplaceAll.Utils
{
public static class UpdateDbContextContents
{
public static void ReplaceAllStringsAcrossTables(IList<ReplaceOp> replaceOps)
{
int dbSetsCount;
using (var uow = new MyContext())
{
dbSetsCount = uow.GetAllDbSets().Count;
}
for (var i = 0; i < dbSetsCount; i++)
{
using (var uow = new MyContext()) // using a new context each time to free resources quickly.
{
var dbSetResult = uow.GetAllDbSets()[i];
var stringProperties = dbSetResult.DbSetType.GetProperties()
.Where(p => p.PropertyType == typeof(string))
.ToList();
var dbSetEntities = dbSetResult.DbSet;
var haveChanges = false;
foreach (var entity in dbSetEntities)
{
foreach (var stringProperty in stringProperties)
{
var oldPropertyValue = stringProperty.GetValue(entity, null) as string;
if (string.IsNullOrWhiteSpace(oldPropertyValue))
{
continue;
}
var newPropertyValue = oldPropertyValue;
foreach (var replaceOp in replaceOps)
{
newPropertyValue = newPropertyValue.ReplaceString(replaceOp.ToFind, replaceOp.ToReplace, replaceOp.Comparison);
}
if (oldPropertyValue != newPropertyValue)
{
stringProperty.SetValue(entity, newPropertyValue, null);
haveChanges = true;
}
}
}
if (haveChanges)
{
uow.SaveChanges();
}
}
}
}
}
} using System;
using System.Text;
namespace EFReplaceAll.Utils
{
public static class StringExtensions
{
public static string ReplaceString(this string src, string oldValue, string newValue, StringComparison comparison)
{
if (string.IsNullOrWhiteSpace(src))
{
return src;
}
if (string.Compare(oldValue, newValue, comparison) == 0)
{
return src;
}
var sb = new StringBuilder();
var previousIndex = 0;
var index = src.IndexOf(oldValue, comparison);
while (index != -1)
{
sb.Append(src.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;
previousIndex = index;
index = src.IndexOf(oldValue, index, comparison);
}
sb.Append(src.Substring(previousIndex));
return sb.ToString();
}
}
} UpdateDbContextContents.ReplaceAllStringsAcrossTables(
new[]
{
new ReplaceOp
{
ToFind = "https://www.dntips.ir",
ToReplace = "https://www.dntips.ir",
Comparison = StringComparison.OrdinalIgnoreCase
},
new ReplaceOp
{
ToFind = "https://www.dntips.ir",
ToReplace = "https://www.dntips.ir",
Comparison = StringComparison.OrdinalIgnoreCase
}
});