سازگار سازی EFTracingProvider با EF Code first
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۵/۲۱ ۱۴:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.wrappedConnection != null)
this.wrappedConnection.Dispose();
}
base.Dispose(disposing);
}
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Diagnostics;
using System.Linq;
using EFTracingProvider;
namespace Sample
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
var className = this.ContextType.FullName;
var connectionStringData = ConfigurationManager.ConnectionStrings[className];
if (connectionStringData == null)
throw new InvalidOperationException(string.Format("ConnectionStrings[{0}] not found.", className));
TargetDatabase = new DbConnectionInfo(connectionStringData.ConnectionString, connectionStringData.ProviderName);
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(MyContext context)
{
for (int i = 0; i < 7; i++)
context.Users.Add(new Person { Name = "name " + i });
base.Seed(context);
}
}
public class MyContext : MyLoggedContext
{
public DbSet<Person> Users { get; set; }
}
public abstract class MyLoggedContext : DbContext
{
protected MyLoggedContext()
: base(existingConnection: createConnection(), contextOwnsConnection: true)
{
var ctx = ((IObjectContextAdapter)this).ObjectContext;
ctx.GetTracingConnection().CommandExecuting += (s, e) =>
{
Console.WriteLine("{0}\n", e.ToTraceString());
};
}
private static DbConnection createConnection()
{
var st = new StackTrace();
var sf = st.GetFrame(2); // Get the derived class Type in a base class static method
var className = sf.GetMethod().DeclaringType.FullName;
var connectionStringData = ConfigurationManager.ConnectionStrings[className];
if (connectionStringData == null)
throw new InvalidOperationException(string.Format("ConnectionStrings[{0}] not found.", className));
if (!isEFTracingProviderRegistered())
EFTracingProviderConfiguration.RegisterProvider();
EFTracingProviderConfiguration.LogToFile = "log.sql";
var wrapperConnectionString =
string.Format(@"wrappedProvider={0};{1}", connectionStringData.ProviderName, connectionStringData.ConnectionString);
return new EFTracingConnection { ConnectionString = wrapperConnectionString };
}
private static bool isEFTracingProviderRegistered()
{
var data = (DataSet)ConfigurationManager.GetSection("system.data");
var providerFactories = data.Tables["DbProviderFactories"];
return providerFactories.Rows.Cast<DataRow>()
.Select(row => (string)row.ItemArray[1])
.Any(invariantName => invariantName == "EF Tracing Data Provider");
}
}
public static class Test
{
public static void RunTests()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
using (var ctx = new MyContext())
{
var users = ctx.Users.AsEnumerable();
if (users.Any())
{
foreach (var user in users)
{
Console.WriteLine(user.Name);
}
}
var rnd = new Random();
var user1 = ctx.Users.Find(1);
user1.Name = "test user " + rnd.Next();
ctx.SaveChanges();
}
}
}
}
public class MyContext : MyLoggedContext
insert [dbo].[People]([Name]) values (@0) select [Id] from [dbo].[People] where @@ROWCOUNT > 0 and [Id] = scope_identity() -- @0 (dbtype=String, size=-1, direction=Input) = "name 0" insert [dbo].[People]([Name]) values (@0) select [Id] from [dbo].[People] where @@ROWCOUNT > 0 and [Id] = scope_identity() -- @0 (dbtype=String, size=-1, direction=Input) = "name 1" insert [dbo].[People]([Name]) values (@0) select [Id] from [dbo].[People] where @@ROWCOUNT > 0 and [Id] = scope_identity() -- @0 (dbtype=String, size=-1, direction=Input) = "name 2" insert [dbo].[People]([Name]) values (@0) select [Id] from [dbo].[People] where @@ROWCOUNT > 0 and [Id] = scope_identity() -- @0 (dbtype=String, size=-1, direction=Input) = "name 3" insert [dbo].[People]([Name]) values (@0) select [Id] from [dbo].[People] where @@ROWCOUNT > 0 and [Id] = scope_identity() -- @0 (dbtype=String, size=-1, direction=Input) = "name 4" insert [dbo].[People]([Name]) values (@0) select [Id] from [dbo].[People] where @@ROWCOUNT > 0 and [Id] = scope_identity() -- @0 (dbtype=String, size=-1, direction=Input) = "name 5" insert [dbo].[People]([Name]) values (@0) select [Id] from [dbo].[People] where @@ROWCOUNT > 0 and [Id] = scope_identity() -- @0 (dbtype=String, size=-1, direction=Input) = "name 6" SELECT [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name] FROM [dbo].[People] AS [Extent1] SELECT [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name] FROM [dbo].[People] AS [Extent1] name 0 name 1 name 2 name 3 name 4 name 5 name 6 update [dbo].[People] set [Name] = @0 where ([Id] = @1) -- @0 (dbtype=String, size=-1, direction=Input) = "test user 1355460609" -- @1 (dbtype=Int32, size=0, direction=Input) = 1