Single Connection String with Multiple Entity Framework Models?

后端 未结 3 2189
孤街浪徒
孤街浪徒 2021-02-08 18:33

At work we currently have a very large web application with a connection to a massive database. We have been using Entity Framework for a while now and to make things easier we

相关标签:
3条回答
  • 2021-02-08 18:49

    Add Default Construction in your Class

    public class ItemContext : DbContext
    {
        public DbSet<Item>Items get; set; }
        public DbSet<ItemDetail> ItemDetails { get; set; }
    
        public ItemContext ()
        {
    
            this.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }
    }
    
    0 讨论(0)
  • 2021-02-08 19:01

    This is how I have implemented my solution to this problem:

    namespace DBLibrary
    {
        public enum Models
        {
            Model1,
            Model2    
        }
    
        public static class Database
        {
            public static string EntitiesConnectionString(Models model)
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
    
                builder["MultipleActiveResultSets"] = true;
                builder["Connect Timeout"] = 30;
    
                EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
                entityBuilder.Provider = "System.Data.SqlClient";
                entityBuilder.ProviderConnectionString = builder.ConnectionString;
    
                switch (model)
                {
                    case Models.Model1:
                        entityBuilder.Metadata = "res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
                        return entityBuilder.ToString();
                    case Models.Model2:
                        entityBuilder.Metadata = "res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl";
                        return entityBuilder.ToString();
                    default:
                        throw new Exception("Invalid model, no connection string defined");
                }
            }
        }
    }
    

    I still need to clean up the code and all but I think this give you a good idea on how this can be implemented. I would still be very interested if there are different and better ways of doing this.

    Thanks!

    0 讨论(0)
  • 2021-02-08 19:10

    I had the same problem. I have solved it by following way:

    I have created two edmx file, but while creating second edmx file, i ignored the connection string to be save in config file. This way my config file will hold only one Connection string. Then i modified following lines in my connection string:

    <add name="MyDbContext" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string=&quot;data source=abc;initial catalog=mydb;persist security info=True;user id=myuser;password=password;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    

    Just replace "res://model1.csdl" with "res://*/" and it works like a charm.

    You can specify this connection name in constructor of your dbcontext class like:

    public MyDbContext() : base("name=NameOfYourConnectionString") //  Name of your connection string
    { }
    

    Note: I am using Entity Framework 5.0.

    0 讨论(0)
提交回复
热议问题