none
Using Entity Framework 4.1 Code First with Existing Database and Dynamic ConnectionString RRS feed

  • Question

  • I'm using Entity Framework 4.1 Code First with existing DB (I know, technically not Code-First) and I'm trying to set the connection to the database with dynamic connectionstring. It doesn't matter which way I try, I always end up with the error "Failed to get the MetadataWorkspace for the DbContext type ...". I have an object which will give me the required connectionstring but the question is that how should I use it to make it work? I tried to set the Database.DefaultConnectionFactory to a new SqlConnectionFactory which I initialized with this connectionstring but it still didn't work until I set the correct database name in the constructor (... : base("DBName")). This solution is not acceptable in my case.
    Wednesday, September 28, 2011 1:31 PM

Answers

  • You can set the connection string in the constructor of your dbcontext class like you described.

    If you are using am existing database, try to disable IncludeMetaData. I am not sure, but the error you get, sounds like.. Here an example how you disable it. You need to override OnModelCreating to setup this convention.

    public class YourContext : DbContext
     {
        
        public YourContext (ISharedConfiguration configuration)
                : base(configuration.ConnectionString)
            {
                _configuration = configuration;
            }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
     modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
     }
    
    }

    Greets
    Holger

     

     

    • Marked as answer by Antti Simonen Wednesday, September 28, 2011 2:02 PM
    Wednesday, September 28, 2011 1:49 PM

All replies

  • You can set the connection string in the constructor of your dbcontext class like you described.

    If you are using am existing database, try to disable IncludeMetaData. I am not sure, but the error you get, sounds like.. Here an example how you disable it. You need to override OnModelCreating to setup this convention.

    public class YourContext : DbContext
     {
        
        public YourContext (ISharedConfiguration configuration)
                : base(configuration.ConnectionString)
            {
                _configuration = configuration;
            }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
     modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
     }
    
    }

    Greets
    Holger

     

     

    • Marked as answer by Antti Simonen Wednesday, September 28, 2011 2:02 PM
    Wednesday, September 28, 2011 1:49 PM
  • Thank you! Your solution helped me to solve it. So I didn't need to set DefaultConnectionFactory at all. Just passing the connectionstring to DbContext in the constructor was all that was required. I didn't even need to remove the MetadataConvention although it's probably better to remove since I won't be needing it anyhow. Thanks again.
    Wednesday, September 28, 2011 2:10 PM
  • great :)

    have a nice day.

    Wednesday, September 28, 2011 2:12 PM