1

I'm trying to create a simple Unit test on a projet in order to test my connection to my database, but each time i run my test, the DbContext throw me the following error : The function requires all thread to run.

Here my unit test :

[TestMethod]
        public void CanReadObjectFromContext()
        {
            using (var context = new DbAccess())
            {
                var blogger = context.Set<Blog>().ToList();
                Assert.IsTrue(blogger.Any());
            }
        }

Here my DbContext

public class DbAccess : BaseContext<DbAccess>
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new BlogConfiguration());
        }

        private void ApplyRules()
        {
            foreach (var entry in this.ChangeTracker.Entries().Where(e=>e.Entity is IAuditInfo && (e.State == EntityState.Added || e.State == EntityState.Modified)))
            {
                var e = (IAuditInfo) entry.Entity;
                if (entry.State == EntityState.Added)
                    e.CreatedOn = DateTime.Now;

                e.ModifiedOn = DateTime.Now;
            }
        }

        public override int SaveChanges()
        {
            ApplyRules();
            return base.SaveChanges();
        }
    }

And my base class for my DbContext

public class BaseContext<TContext> : DbContext where TContext : DbContext
    {
        static BaseContext()
        {
            // On s'assure que le BdContext ne va pas faire d'action de modifciationsur notre base de données
            // Ici nous avons déjà une base de données existante
            Database.SetInitializer<TContext>(null);
        }

        protected BaseContext()
            : base(ConnectionStringName)
        {

        }

        public static string ConnectionStringName
        {
            get
            {
                if (ConfigurationManager.AppSettings["ConnectionStringName"] != null)
                    return ConfigurationManager.AppSettings["ConnectionStringName"];

                return "SqlConnection";
            }
        }
    }

Thank for your help. Anthony

  • Isn't your test actually testing whether a connection can be established, that a `Blog` entity can be selected and that the table isn't empty? Do you have a stack trace? – Mathieu Guindon Jul 23 '13 at 02:52
  • Yes, i want to test the connection and at the same time i want to see if my entity 'Blog' contain some value. But each time i use my context to grap the value of my entites i got a exception saying that ((System.Data.Entity.DbContext)(context)).System.Data.Entity.Infrastructure.IObjectContextAdapter.ObjectContext : the function evaluation requires all thrads to run – anthony paroutaud Jul 23 '13 at 03:09
  • Maybe this can help? http://stackoverflow.com/questions/4460206/lazyt-the-function-evaluation-requires-all-threads-to-run. Do you have a listing for the `Blog` class? – Mathieu Guindon Jul 23 '13 at 03:27
  • Are you using EF 6.0? I'm getting the same issue with a recent test project. The only difference between my production code (which works) and the test project (which doesn't) is the EF version. My unconfirmed guess is the new version of EF added some Lazy functions which cause this issue. – Sailing Judo Aug 07 '13 at 19:26
  • I think I just confirmed my last comment. This is an issue implemented with EF 6.0. Removing EF 6.0 and using EF 5.0 solved this problem. I'm sure there is a way to make it work in EF 6, but who needs the hassle? – Sailing Judo Aug 07 '13 at 20:06

0 Answers0