具有多个实体框架模型的单连接字符串? [英] Single Connection String with Multiple Entity Framework Models?

查看:128
本文介绍了具有多个实体框架模型的单连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我们目前拥有一个非常大的Web应用程序,连接到一个庞大的数据库。我们一直在使用实体框架一段时间,并且使事情变得更容易,我们将数据库分为多个实体模型。这对我们来说很好,但我们遇到了一个问题。由于连接字符串的元数据部分,每个EF模型都需要自己的连接字符串。管理这么多连接字符串是一个痛苦。



现在我有一个我认为会奏效的解决方案。我将创建一个类,将元数据信息保存为属性,并将其连接到web.config中的标准连接字符串。所以当我们使用连接字符串Database.EntityConnectionString时,它会给我实体连接字符串,但是我们只需要在web.config中管理一个连接字符串。我们仍然必须使用元数据来管理课程,但是模型不会变化太大,我们不会经常创建它们,因此维护应该是正常的。我的问题是,是否有更好的处理这个问题的方法,或者你怎么做?



谢谢!

 命名空间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 [连接超时] = 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);
}
}
}
}

仍然需要清理代码,除了我认为这给你一个很好的想法如何实现。如果有不同的更好的方法,我仍然会很感兴趣。



谢谢!


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 divided the database into many Entity models. This works well for us but we ran into an issue. Each EF model needs its own connection string due to the metadata part of the connection string. Managing so many connection string is a pain.

Now I have a solution that I think will work. I am going to create a class that will have the metadata info saved as a property also concatenated to the standard connection string in the web.config. So when we use the connection string "Database.EntityConnectionString" it will give me the Entity Connection string but we only have to manage a single connection string in the web.config. We will still have to manage the class with the metadata but Models don't change very much and we don't create them often so maintenance should be fine. My question, is there a better way of dealing with this issue or how would you do it?

Thanks!

解决方案

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!

这篇关于具有多个实体框架模型的单连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆