none
Setting DB Connection in EF with Database first design RRS feed

  • Question

  • I have a EF Model generated from a existing database.

    BL using the EF model is called from a legacy application which doesn't have a configuration file and i couldn't add the EF Entity connection string.

    Trying to figure out a way to add a connection string. I added the following code to the constructor of the Entity class.. When running this implementation i get this error :

    Message = "Keyword not supported: 'metadata'."

    If i remove the MetaData from the connection string then i get this error :

    Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

    ---------------------------------------------------------------Code

    -----------------------------------------------

    public partial class TestQEntities : DbContext
        {

        public TestQEntities(string nameOrConnectionString)
            {
                this.Database.Connection.ConnectionString = nameOrConnectionString;
            }

    }

    }

    _context = new TestQEntities(GetConnectionString());

      private static string GetConnectionString()
            {
                var scsb = new SqlConnectionStringBuilder
                    {
                        DataSource = "(local)",
                        InitialCatalog = "DB",
                        IntegratedSecurity = true
                    };


                var builder = new EntityConnectionStringBuilder
                    {
                        Metadata = "res://*/ROModel.csdl|res://*/ROModel.ssdl|res://*/ROModel.msl",
                        Provider = "System.Data.SqlClient",
                        ProviderConnectionString = scsb.ConnectionString
                    };
                     return builder.ToString() + ";App=EntityFramework";
            }


    Wednesday, March 27, 2013 11:38 PM

All replies

  • Hi  Jega,

    Welcome to the MSDN forum.

    I think you could try to use Entity Framework Power Tools to reverse Code First for existing databases: http://msdn.microsoft.com/en-us/data/jj593170.aspx

    Then you could add a String parameter constructor in dbcontext class.

    public class Sample : DbContext

    {

                public Sample(String connectionString) : base(connectionString)

                {

                }

    }

    Next, you could initialize a dbcontext by passing a connection string.

    Using(var context = new Sample(@”yourconnectionstring”))

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 29, 2013 8:19 AM
  • Hi Alexander,

    Thanks for your help.

    I did the suggested code change .

       public TestQEntities (string nameOrConnectionString) :base(nameOrConnectionString)

           {

                this.Database.Connection.ConnectionString = nameOrConnectionString;
            }

    When i run it get this error :

    Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

    Thanks

    Jega


    Friday, March 29, 2013 4:09 PM
  • I am getting the same error. I am trying to pass in a generated connection string to a non-code first EF 5.0 database/context and getting that bizzare error.
    public partial class ViperEntities
    {
    public ViperEntities(string connectionString) : base(connectionString) { }
    
    }


    William Wegerson (www.OmegaCoder.Com)


    • Edited by OmegaMan Tuesday, August 13, 2013 3:29 PM
    Tuesday, August 13, 2013 3:29 PM