یک دست سازی ی و ک در برنامههای Entity framework 6
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۱/۱۸ ۱۲:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class YeKeInterceptor : IDbCommandInterceptor
{
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
command.ApplyCorrectYeKe();
}
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
command.ApplyCorrectYeKe();
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
command.ApplyCorrectYeKe();
}
} public static class YeKe
{
public const char ArabicYeChar = (char)1610;
public const char PersianYeChar = (char)1740;
public const char ArabicKeChar = (char)1603;
public const char PersianKeChar = (char)1705;
public static string ApplyCorrectYeKe(this object data)
{
return data == null ? null : ApplyCorrectYeKe(data.ToString());
}
public static string ApplyCorrectYeKe(this string data)
{
return string.IsNullOrWhiteSpace(data) ?
string.Empty :
data.Replace(ArabicYeChar, PersianYeChar).Replace(ArabicKeChar, PersianKeChar).Trim();
}
public static void ApplyCorrectYeKe(this DbCommand command)
{
command.CommandText = command.CommandText.ApplyCorrectYeKe();
foreach (DbParameter parameter in command.Parameters)
{
switch (parameter.DbType)
{
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
case DbType.Xml:
parameter.Value = parameter.Value is DBNull ? parameter.Value : parameter.Value.ApplyCorrectYeKe();
break;
}
}
}
} DbInterception.Add(new YeKeInterceptor());
بر روی SqlServer درست کارمی کند ولی بر روی کانکشن SQL CE پیغام زیر را میدهد:
System.NotSupportedException was unhandled by user code
HResult=-2146233067
Message=DesignTimeVisible
Source=EntityFramework.SqlServerCompact
StackTrace:
at System.Data.Entity.SqlServerCompact.SqlCeMultiCommand.set_CommandText(String value)
at EfExt.YeKe.ApplyCorrectYeKe(DbCommand command) in e:\test\EfExt\YeKe.cs:line 33
at EfExt.YeKeInterceptor.ReaderExecuting(DbCommand command, DbCommandInterceptionContext`1 interceptionContext) in e:\MyFilesAndPrograms\WebIO\EfExt\YeKeInterceptor.cs:line 15
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__d(IDbCommandInterceptor i, DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
InnerException: command.CommandText = command.CommandText.ApplyCorrectYeKe();
CREATE PROCEDURE [dbo].[spr_Admin_Replace_Ye_Ke_InAllTables]
AS
BEGIN
BEGIN TRAN
--ی--%u06CC
--ی--%u064A
--ک--%u06A9
--ک--%u0643
DECLARE @Ye_Farsi NCHAR(1), @Ye_Arabi NCHAR(1), @Ke_Farsi NCHAR(1), @Ke_Arabi NCHAR(1)
SET @Ye_Farsi = NCHAR(0X06CC)
SET @Ye_Arabi = NCHAR(0X064A)
SET @Ke_Farsi = NCHAR(0X06A9)
SET @Ke_Arabi = NCHAR(0X0643)
--SELECT @Ye_Farsi, UNICODE(@Ye_Farsi) AS Ye_Farsi_Code, @Ye_Arabi, UNICODE(@Ye_Arabi) AS Ye_Arabi_Code,@Ke_Farsi, UNICODE(@Ke_Farsi) AS Ke_Farsi_Code, @Ke_Arabi, UNICODE(@Ke_Arabi) AS Ke_Arabi_Code
--SELECT * FROM sys.types
DECLARE xcur CURSOR FOR -- a cursor for string columns
SELECT sys.tables.name AS TableName, sys.columns.name AS ColumnName
FROM sys.tables INNER JOIN sys.columns ON sys.tables.object_id = sys.columns.object_id
WHERE sys.columns.system_type_id IN (35, 99, 167, 175, 231, 239)
OPEN xcur
DECLARE @SqlString nvarchar(1000), @TName nvarchar(255), @CName nvarchar(255), @ret int
FETCH NEXT FROM xcur INTO @TName, @CName
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
SET @SqlString = N'UPDATE ' + @TName + ' SET ' + @CName + ' = REPLACE( REPLACE(' + @CName + ',''' + @Ye_Farsi + ''',''' + @Ye_Arabi + ''') ,''' + @Ke_Farsi + ''',''' + @Ke_Arabi + ''')';
EXEC @ret = sp_executesql @SqlString
PRINT @ret
END TRY
BEGIN CATCH
PRINT @SqlString
PRINT ERROR_MESSAGE()
END CATCH
FETCH NEXT FROM xcur INTO @TName, @CName
END
CLOSE xcur
DEALLOCATE xcur
ROLLBACK TRAN
END public class YeKeLetters
{
public static char Ye_Farsi = '\x06CC'; // ی %u06CC
public static char Ye_Arabi = '\x064A'; // ی %u064A
public static char Ke_Farsi = '\x06A9'; // ک %u06A9
public static char Ke_Arabi = '\x0643'; // ک %u0643
} public static string ApplyCorrectYeKe(this string data)
{
return string.IsNullOrWhiteSpace(data)
? data
: data.Replace(ArabicYeChar, PersianYeChar).Replace(ArabicKeChar, PersianKeChar);
} using System;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using DNTPersianUtils.Core; // dotnet add package DNTPersianUtils.Core
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace EFCore3Interceptors
{
public class PersianYeKeCommandInterceptor : DbCommandInterceptor
{
public override InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result)
{
ApplyCorrectYeKe(command);
return result;
}
public override Task<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = new CancellationToken())
{
ApplyCorrectYeKe(command);
return Task.FromResult(result);
}
public override InterceptionResult<int> NonQueryExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<int> result)
{
ApplyCorrectYeKe(command);
return result;
}
public override Task<InterceptionResult<int>> NonQueryExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<int> result,
CancellationToken cancellationToken = new CancellationToken())
{
ApplyCorrectYeKe(command);
return Task.FromResult(result);
}
public override InterceptionResult<object> ScalarExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<object> result)
{
ApplyCorrectYeKe(command);
return result;
}
public override Task<InterceptionResult<object>> ScalarExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<object> result,
CancellationToken cancellationToken = new CancellationToken())
{
ApplyCorrectYeKe(command);
return Task.FromResult(result);
}
private static void ApplyCorrectYeKe(DbCommand command)
{
command.CommandText = command.CommandText.ApplyCorrectYeKe();
foreach (DbParameter parameter in command.Parameters)
{
switch (parameter.DbType)
{
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
case DbType.Xml:
parameter.Value = parameter.Value is DBNull ? parameter.Value : parameter.Value.ToString().ApplyCorrectYeKe();
break;
}
}
}
}
} namespace EFCore3Interceptors
{
public class BloggingContext : DbContext
{
// ...
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder
.UseSqlServer("...")
.AddInterceptors(new PersianYeKeCommandInterceptor());
}
}
}
} parameter.Value = parameter.Value is DBNull ? parameter.Value : parameter.Value.ToString().ApplyCorrectYeKe();
if (!(parameter.Value is DBNull) && parameter.Value is string)
{
parameter.Value = Convert.ToString(parameter.Value, CultureInfo.InvariantCulture).ApplyCorrectYeKe();
}