对于实体框架连接字符串 [英] Connection Strings for Entity Framework

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

问题描述

我想分享横跨在Silverlight多个实体相同的数据库信息..但我想连接字符串被命名为XYZ,并让每个人都获得了来自machine.config中...


$连接字符串b $ b

实体的元数据部分是不同的,因为我没有命名实体一样..



我可以把多个实体元数据部分?



下面是一个例子。我想使用此连接字符串,但请注意,我把多个实体的元数据部分。



基本上,我想利用这个连接字符串

 <添加名称=XYZ的connectionString =元数据= RES://*/ModEntity.csdl |高分辨率://*/ModEntity.ssdl |高分辨率://*/ModEntity.msl;提供商= System.Data.SqlClient的;提供连接字符串=安培; QUOT;数据源= SomeServer;初始目录= SomeCatalog;坚持安全信息= TRUE;用户ID =实体;密码= SomePassword; MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/> 

和此连接字符串

 <添加名称=XYZ的connectionString =元数据= RES://*/Entity.csdl |高分辨率://*/Entity.ssdl |高分辨率://*/Entity.msl;供应商= System.Data.SqlClient的;提供连接字符串=安培; QUOT;数据源= SOMESERVER;初始目录= SOMECATALOG;坚持安全信息= TRUE;用户ID =实体;密码=实体; MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/> 



为了使这个连接字符串

 <添加名称=XYZ的connectionString =元数据= RES://*/Entity.csdl |高分辨率://*/Entity.ssdl |高分辨率://*/Entity.msl |高分辨率://*/ModEntity.csdl |高分辨率://*/ModEntity.ssdl |高分辨率://*/ModEntity.msl;提供商= System.Data.SqlClient的;提供连接字符串=安培; QUOT;数据源= SOMESERVER;初始目录= SOMECATALOG;坚持安全信息= TRUE;用户ID =实体;密码= SOMEPASSWORD; MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/> 



但它根本不起作用。 。无论是项目能够连接到它

 字符串encConnection = ConfigurationManager.ConnectionStrings [connectionName中] .ConnectionString; 
型contextType = typeof运算(test_Entities);
对象objContext = Activator.CreateInstance(contextType,encConnection);
返回objContext为test_Entities;


解决方案

不幸的是,多个实体上下文合并成一个单一命名的连接是不可能的。如果你想使用命名连接字符串从config文件来定义你的实体框架连接,它们将各自必须有一个不同的名称。按照惯例,该名称通常是上下文的名称:

 <添加名称=ModEntity的connectionString =元数据= RES://*/ModEntity.csdl |高分辨率://*/ModEntity.ssdl |高分辨率://*/ModEntity.msl;提供商= System.Data.SqlClient的;提供连接字符串=安培; QUOT;数据源= SomeServer ;初始目录= SomeCatalog;坚持安全信息= TRUE;用户ID =实体;密码= SomePassword; MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/> 
<添加名称=实体的connectionString =元数据= RES://*/Entity.csdl |高分辨率://*/Entity.ssdl |高分辨率://*/Entity.msl;供应商=系统.Data.SqlClient;提供连接字符串=安培; QUOT;数据源= SOMESERVER;初始目录= SOMECATALOG;坚持安全信息= TRUE;用户ID =实体;密码=实体; MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/>



不过,如果你最终的命名空间冲突,你可以使用任何你想要的名称和简单地传递正确的名称上下文时产生的:

  VAR背景=新的实体(EntityV2); 



显然,如果您使用的是一个工厂或依赖注入产生的背景这一策略效果最好。



另一种选择是,以编程方式产生各方面的整个连接字符串,然后通过整个字符串中的构造(不只是名称)。

  //获取数据源= SomeServer ...
VAR innerConnectionString = GetInnerConnectionStringFromMachinConfig();
//构建实体框架连接字符串。
变种的connectionString = CreateEntityConnectionString(实体,innerConnectionString);
VAR背景=新的EntityContext(的connectionString);



如何是这样的:

 键入contextType = typeof运算(test_Entities); 
串innerConnectionString = ConfigurationManager.ConnectionStrings [内在]的ConnectionString。
串entConnection =
的String.Format(
元= RES:// * / {0} .csdl | RES:// * / {0} .ssdl |资源:// * / {0} .msl;提供商= System.Data.SqlClient的;提供连接字符串= \{1} \,
contextType.Name,
innerConnectionString);
对象objContext = Activator.CreateInstance(contextType,entConnection);
返回objContext为test_Entities;



...在你的machine.config以下内容:

 <添加名称=内在的connectionString =数据源= SomeServer;初始目录= SomeCatalog;坚持安全信息= TRUE;用户ID =实体;密码= SomePassword ; MultipleActiveResultSets = TRUE的providerName =System.Data.SqlClient的/> 

这方法,你可以在机器上的每一个项目使用一个连接字符串为每个上下文。


I want to share same Database information across multiple entities in Silverlight.. but I want the connection string to be named xyz and have everyone access that connection string from machine.config...

The meta data part of the entities will be different since I didn't name the entities the same..

Can I put multiple entities in that metadata section?

Here is an example.. I want to use this connection string but note that i put multiple entities in the metadata section..

Basically I want to take this Connection String

<add name="XYZ" connectionString="metadata=res://*/ModEntity.csdl|res://*/ModEntity.ssdl|res://*/ModEntity.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

And this Connection String

 <add name="XYZ" connectionString="metadata=res://*/Entity.csdl|res://*/Entity.ssdl|res://*/Entity.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=SOMESERVER;Initial Catalog=SOMECATALOG;Persist Security Info=True;User ID=Entity;Password=Entity;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

To make this Connection String

<add name="XYZ" connectionString="metadata=res://*/Entity.csdl|res://*/Entity.ssdl|res://*/Entity.msl|res://*/ModEntity.csdl|res://*/ModEntity.ssdl|res://*/ModEntity.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=SOMESERVER;Initial Catalog=SOMECATALOG;Persist Security Info=True;User ID=Entity;Password=SOMEPASSWORD;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

But it simply doesn't work. Neither project can connect to it.

string encConnection = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
Type contextType = typeof(test_Entities);
object objContext = Activator.CreateInstance(contextType, encConnection);
return objContext as test_Entities; 

解决方案

Unfortunately, combining multiple entity contexts into a single named connection isn't possible. If you want to use named connection strings from a .config file to define your Entity Framework connections, they will each have to have a different name. By convention, that name is typically the name of the context:

<add name="ModEntity" connectionString="metadata=res://*/ModEntity.csdl|res://*/ModEntity.ssdl|res://*/ModEntity.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
<add name="Entity" connectionString="metadata=res://*/Entity.csdl|res://*/Entity.ssdl|res://*/Entity.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=SOMESERVER;Initial Catalog=SOMECATALOG;Persist Security Info=True;User ID=Entity;Password=Entity;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

However, if you end up with namespace conflicts, you can use any name you want and simply pass the correct name to the context when it is generated:

var context = new Entity("EntityV2");

Obviously, this strategy works best if you are using either a factory or dependency injection to produce your contexts.

Another option would be to produce each context's entire connection string programmatically, and then pass the whole string in to the constructor (not just the name).

// Get "Data Source=SomeServer..."
var innerConnectionString = GetInnerConnectionStringFromMachinConfig();
// Build the Entity Framework connection string.
var connectionString = CreateEntityConnectionString("Entity", innerConnectionString);
var context = new EntityContext(connectionString);

How about something like this:

Type contextType = typeof(test_Entities);
string innerConnectionString = ConfigurationManager.ConnectionStrings["Inner"].ConnectionString;
string entConnection = 
    string.Format(
        "metadata=res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl;provider=System.Data.SqlClient;provider connection string=\"{1}\"",
        contextType.Name,
        innerConnectionString);
object objContext = Activator.CreateInstance(contextType, entConnection);
return objContext as test_Entities; 

... with the following in your machine.config:

<add name="Inner" connectionString="Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

This way, you can use a single connection string for every context in every project on the machine.

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

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