Database Container is required error for .dbf files.

Mar 3, 2014 at 2:04 PM
I am working on project that needs to get data from .dbf files using EF6. I am trying your provider and getting "Database Container is required". I reflected your code and seems like it throws ApplicationException("Database Container is required") for the file names that does not contain ".dbc". Am I correct? Or did I miss anything and is there any way to work with .dbf files?

Thanks, Eugen.
Coordinator
Mar 3, 2014 at 2:21 PM
A DBC is only required when using Code First to generate your data structure. In your case, you are working with an existing data structure so you will want to instruct EF to not generate one for you. The way I do that is I create a static constructor on my DataContext and set the database initializer to null.

Example:
public class MyDataContext : DbContext {
   static MyDataContext() {
       Database.SetInitializer<MyDataContext>(null);
   }
}
Mar 3, 2014 at 2:29 PM
I'm using the following context implementation. Should it work?
internal class DbfDataContext : BaseDataContext
    {
        public DbfDataContext( string connectionString )
            : base( b =>
                {
                    b.Configurations.Add( new DbfMapping() );
                }, new VfpConnection( connectionString ) )
        {

        }
    }
public class BaseDataContext : DbContext, IDataContext
    {
private Action<DbModelBuilder> initializer;

public BaseDataContext( Action<DbModelBuilder> initializer, VfpConnection connection )
            : base( connection, true )
        {
            this.initializer = initializer;
        }
protected override void OnModelCreating( DbModelBuilder modelBuilder )
        {
            initializer( modelBuilder );
        }
}
Coordinator
Mar 3, 2014 at 2:35 PM
You need to add Database.SetInitializer<DbfDataContext>(null) to prevent EF from trying to create tables for you.
internal class DbfDataContext : BaseDataContext
   {
        // Add this static constructor
        static DbfDataContext() 
        {
            Database.SetInitializer<DbfDataContext>(null);
        }
    
        public DbfDataContext( string connectionString )
            : base( b =>
                {
                    b.Configurations.Add( new DbfMapping() );
                }, new VfpConnection( connectionString ) )
        {

        }
    }
Marked as answer by EugenSorin on 3/3/2014 at 7:55 AM
Mar 3, 2014 at 3:55 PM
Thank you, TomBrothers.
Mar 19, 2014 at 4:25 PM
Hi TomBrothers,
I'm not sure if my next issue related to the Vfp or not. But I will be very appreciate if you can help me. So I use this datacontext to get data from several free tables one by one. Every datacontext creation I wrap into 'using' statement but seems like next datacontext uses connection and returns data from previous one because the first one has not been disposed. Another interesting case is that I can't create a new datacontext for 15 minutes if I'm not mistaken. I suppose that I need to decrease Connection timeout somehow but it is a readonly property.
Coordinator
Mar 20, 2014 at 12:01 AM
I haven't experience that type of issue. Does you connection string set the connection to exclusive? I can try to help you debug the issue if you can send me an example project. You can email it to me at tombrothers@outlook.com.