Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hikalkan/17457ec9c972e4d2a333 to your computer and use it in GitHub Desktop.
Save hikalkan/17457ec9c972e4d2a333 to your computer and use it in GitHub Desktop.
Effort.Ef6 & EntityFramework.DynamicFilters combine problem
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using EntityFramework.DynamicFilters; //to use EntityFramework.DynamicFilters
//using EntityFramework.Filters; //Enable to use EntityFramework.Filters
using Xunit;
namespace EffortDynamicFiltersTest
{
class Program
{
static void Main(string[] args)
{
DropAndInitializeDatabase();
using (var dbContext = new MyDbContext(GetConnectionString()))
{
var myService = new MyService(dbContext);
var people = myService.GetAllPeople();
foreach (var person in people)
{
Console.WriteLine(person);
}
}
DeleteDatabaseFile();
Console.WriteLine();
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
}
private static void DropAndInitializeDatabase()
{
DeleteDatabaseFile();
using (var dbContext = new MyDbContext(GetConnectionString()))
{
InitialData.Populate(dbContext);
dbContext.SaveChanges();
}
}
public static string GetConnectionString()
{
return "Data Source=" + GetDatabasePath() + "; Persist Security Info=False;";
}
private static void DeleteDatabaseFile()
{
if (File.Exists(GetDatabasePath()))
{
File.Delete(GetDatabasePath());
}
}
public static string GetDatabasePath()
{
return @"C:\MyDatabase.sdf";
}
}
public class MyService
{
private readonly MyDbContext _dbContext;
public MyService(MyDbContext dbContext)
{
_dbContext = dbContext;
}
public List<Person> GetAllPeople()
{
return _dbContext.People.ToList();
}
}
public class MyDbContext : DbContext
{
public virtual IDbSet<Person> People { get; set; }
public MyDbContext(string connectionString)
: base(connectionString)
{
//this.EnableFilter("SoftDeleteFilter"); //Enable to use EntitiyFramework.Filters
}
public MyDbContext(DbConnection dbConnection)
: base(dbConnection, false)
{
//this.EnableFilter("SoftDeleteFilter"); //Enable to use EntitiyFramework.Filters
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
/* Enable to use EntityFramework.DynamicFilters */
modelBuilder.Filter("SoftDeleteFilter", (ISoftDelete d) => d.IsDeleted, false);
/* Enable to use EntitiyFramework.Filters */
//DbInterception.Add(new FilterInterceptor());
//modelBuilder.Conventions.Add(
// FilterConvention.Create<ISoftDelete>("SoftDeleteFilter", (e) => e.IsDeleted == false)
// );
}
}
public class Person : ISoftDelete
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual bool IsDeleted { get; set; }
public override string ToString()
{
return String.Format("[Person {0}] {1} ({2})", Id, Name, IsDeleted ? "DELETED" : "NOT DELETED");
}
}
public interface ISoftDelete
{
bool IsDeleted { get; set; }
}
public static class InitialData
{
public static void Populate(MyDbContext dbContext)
{
dbContext.People.Add(new Person { Name = "asimov" });
dbContext.People.Add(new Person { Name = "adams", IsDeleted = true });
}
}
public class MyTests
{
[Fact]
public void Test_GetPeople()
{
using (var dbConnection = Effort.DbConnectionFactory.CreateTransient())
{
using (var dbContext = new MyDbContext(dbConnection))
{
InitialData.Populate(dbContext);
dbContext.SaveChanges();
var myService = new MyService(dbContext);
var people = myService.GetAllPeople();
Assert.Equal(1, people.Count);
Assert.Equal("asimov", people[0].Name);
}
}
}
}
}
System.NullReferenceExceptionObject reference not set to an instance of an object.
at Effort.Internal.CommandActions.DbCommandActionHelper.FormatParameters(IList`1 source, IList`1 description, ITypeConverter converter)
at Effort.Internal.CommandActions.QueryCommandAction.ExecuteDataReader(ActionContext context)
at Effort.Provider.EffortEntityCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(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)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList(IEnumerable`1 source)
at EffortDynamicFiltersTest.MyService.GetAllPeople() in Program.cs: line 79
at EffortDynamicFiltersTest.MyTests.Test_GetPeople() in Program.cs: line 154
@hikalkan
Copy link
Author

I'm using Effort.Ef6 and EntityFramework.DynamicFilters together but getting exception as shown above.

Used tools:

Not working case:

  • When I run the unit test it throws the exception I shared above. When combining EntityFramework.DynamicFilters with Effort.EF6 it's not working.

Working cases:

  • When I run the console application, everything works. So, DynamicFilters seems working in production code. (I'm using it in a bigger product with SQL Server 2012 and still working)
  • When I change EntityFramework.DyanmicFilters to EntityFramework.Filters (https://github.com/jbogard/EntityFramework.Filters). So, Effort.EF6 is also working (I'm using it in a bigger project with many tests, it's working).

So, there is a problem when using they together. What can be the problem?

_You can download the application here:_

https://www.dropbox.com/s/6jllfn0hfprb5gc/EffortDynamicFiltersTest.zip?dl=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment