夕颜

女人如烟

2011年1月12日

Unable to add data connection The given key was not present in dictionary

  在VS2010的Data Source或者Server Explorer面板中添加Data Source的过程中,出现“Unable to add data connection The given key was not present in dictionary”的错误提示。

  解决方案:http://connect.microsoft.com/VisualStudio/feedback/details/552006/unable-to-create-data-connection-in-server-explorer 

  具体操作:

  edit C:\Users\...\AppData\Roaming\Microsoft\VisualStudio\10.0\ServerExplorer\DefaultView.SEView and remove the connection with the wrong Provider. If you do not know which of the provider is failing, simply delete the file :)

  If this won't help try deleting also C:\Users\...\AppData\Local\Microsoft\VisualStudio\10.0 

 

  编辑上文提到的“DefaultView.SEView”文件,删除里面错误的数据链接适配器,如果不知道哪个适配器是错误的,可以索性把这个文件删了完事,如果还出现这个错误,那么直接把上层的“10.0”文件夹也给删了就行。重启VS2010时会重新创建这个文件或文件夹。建议提前备份一下这个文件夹,以免出现其他问题。

posted @ 2011-01-12 22:54 夕颜 阅读(130) 评论(0) 编辑

2009年8月10日

Only parameterless constructors and initializers are supported in LINQ to Entities

近期开发过程中,使用Entity Framework时,在对一个实体通过主键(GUID类型)查询时,出现“Only parameterless constructors and initializers are supported in LINQ to Entities”错误,代码如下:
var roles = from r in entites.Roles
                where r.RoleId == new Guid(roleId)
                select r;
而var roles = from r in entites.Roles
                where r.RoleId == new Guid(“fe3f46fd-b85a-42f5-a490-fd76e249f045”)
                select r;
这样则没有问题,我仔细对比了roleId的值,实在是没发现问题,最终google出两篇文章还算可以:
http://blog.miniasp.com/?tag=/linq+to+entity 
http://geekswithblogs.net/SudheersBlog/archive/2009/06/11/132758.aspx 
链接2指出问题的关键:有些CLR方法当前并不能转化为规范的表达式树方法(翻译的不当:Certain CLR methods are converted to command tree canonical functions, which can be executed on the database. If a CLR method cannot be mapped to a command tree canonical function, an exception will be thrown when translation occurs.),反正内意思就是有些不支持,就是不能用,哈哈。
微软提供了一个可用CLR方法的列表《CLR Method to Canonical Function Mapping》,链接:
http://msdn.microsoft.com/en-us/library/bb738681.aspx

posted @ 2009-08-10 16:43 夕颜 阅读(173) 评论(0) 编辑

2009年8月6日

Entity Framework中出现"已有打开的与此命令相关联的 DataReader,必须首先将它关闭。"的解决方案

      前期搭建一个ASP.NET MVC应用的框架过程中,使用了Entity Framework框架作为数据处理部分。其中在Linq to Entity操作过程中偶出现 “已有打开的与此命令相关联的 DataReader,必须首先将它关闭。”该问题,经查善友老大的文章找到一链接,得以解决该问题:http://www.netknowledgenow.com/blogs/onmaterialize/archive/2006/09/20/Fixing-the-_2200_There-is-already-an-open-DataReader-associated-with-this-Command-which-must-be-closed-first_2E002200_-exception-in-Entity-Framework.aspx
      下面摘录该博文内容:

Fixing the "There is already an open DataReader associated with this Command which must be closed first." exception in Entity Framework

Once you get your model, database and mapping metadata files configured correctly for Entity Framework, one of the first exceptions that you're likely to face is the DataReader already open exception. It will normally surface when you're iterating over a result set and then lazy loading a child collection. In the following example I have a Contact entity that has a Phone collection. If I lazy load the Phones then I'll get the DataReader exception.

                var contacts = from c in db.Contact
select c;
foreach (Contact c in contacts)
{
if (c.Phones.IsLoaded == false)
c.Phones.Load();
if (c.Phones.Count > 0)
{
Console.WriteLine(c.LastName);
foreach (ContactPhone p in c.Phones)
{
Console.WriteLine("\t"+p.ContactPhoneId);
}
}
}

The reason for the exception is that there is an open data reader while reading in the contacts and then the loading of the child Phone collection is attempting to open another DataReader. By default, the SqlClient ADO.NET driver does not allow this.

There are two ways to fix this. First, if you are using SQL Server 2005, you can just enable MARS in your connection string.

add name="YourDBConnectionString" connectionString="metadata=.;
	provider=System.Data.SqlClient;provider connection string="Data Source=irv-dev-vms05;Initial Catalog=YourDB;Integrated Security=True;
	MultipleActiveResultSets=true"" providerName="System.Data.Mapping" /

A second option is to read all the results into memory in one shot and close the connection. Then you are free to open another connection to the database to read in the child entities. So how do you control this? The trip to the database does not happen when the LINQ statement is assigned to the var in the code above. Instead, the query to the database happens during the inplicit call to the enumerator of the contacts collection. To force all the contacts to be loaded, simply copy them to a list in memory using the ToList method and then continue the normal processing.

                var contacts = from c in db.Contact
select c;
List results = contacts.ToList();
foreach (Contact c in results)
{
if (c.Phones.IsLoaded == false)
c.Phones.Load();
if (c.Phones.Count > 0)
{
Console.WriteLine(c.LastName);
foreach (ContactPhone p in c.Phones)
{
Console.WriteLine("\t"+p.ContactPhoneId);
}
}
}

This works as expected. Another important realization here is that there is an open connection to the database as long as you are reading in records through that enumerator! A novice programmer may unwittingly include a massive number of time consuming operations within the foreach loop (calling a web service, for example, comes to mind) which will keep the connection to the database open for an inordinate length of time. Of course, you won't fall into that trap.

*********分隔符*********

文中提到解决方案有两种:

1、数据库为SQL Server 2005版本时,可以在web.config数据库链接串中加入MultipleActiveResultSets=true。

2、进行重复操作之前,将数据查询结果放入内存中,再进行使用。

具体解释看上文。

posted @ 2009-08-06 01:38 夕颜 阅读(995) 评论(0) 编辑

2009年1月5日

Asp.net + FluorineFx 开发笔记-配置问题

   半年前研究了一阵子Flex,当时了解到了FluorineFx,这个东西配置的时候有些道道,前一阵子配置成功了,但是由于忙于其他项目,Flex的研究告一段落。当前有个小应用,我突然想到还是用Flex吧,于是又拾了起来,突然发现我脑子真的很不行···,配置的时候又出现初学时的问题了。我居然一点都不记得当时怎么搞的了,为了预防以后类似事件的再次发生,我还是记下来吧,唉·····开始吧...

   首先得注意的就是需要理清你的Web项目目录和Flex源代码的目录关系。.net根据FluorineFx建项目的过程我就不写了,太简单了,web项目我是建的http,不是文件系统,这样可以预防以后的发布端口问题。

   我当前是Web项目和Flex项目在同一级目录下。

   Flex项目建设过程:

   1、New Flex Project: 界面中的Project location - Folder 目录选择框为Flex源码目录,例如D:\PerformanceEvaluationSystem\PES.Flex。Next···

   2、Configure ASP.NET Server: 界面中的Server框中我选择的是Use InternetInformation Services(IIS),因为我的ASP.NET项目为HTTP类型。然后Server location框中的Web application root:为Web项目文件目录,例如D:\PerformanceEvaluationSystem\PES.WebWeb application URL:为Web地址,例如http://localhost/PES.Web/;然后可以点Validate Configuration校验一下。下面的Complied Flex application location框中为Flex编译之后的目的路径,类似我们.net中的bin文件夹,这个路径我是放在了我的Web目录下的一个文件夹中了,例如D:\PerformanceEvaluationSystem\PES.Web\Flex 然后Finish就好了。

   然后我们打开新建的Flex项目的属性页,点Flex Compiler,从右面找到Complier options框,这个框中最下方有个Additional compiler arguments,输入-locale en_US -services "..\..\PES.Web\WEB-INF\flex\services-config.xml"  我出问题就是出在这里,编译的时候老是通不过,突然想到,这个“”里面的services-config.xml文件的路径是相对与Flex源代码的,也就是PES.Flex\src下的文件与services-config.xml的相对路径,这时候需要将Flex项目和Web项目的相对位置也考虑进去,注意这里就不会出现“unable to open ‘甚么甚么/services-config.xml’ ” 的问题了。

   把Flex Server中的Context root设置成你的虚拟目录名称,例如/PES.Web

   这样在开发过程中就ok了,部署我还没有试验。

 

posted @ 2009-01-05 00:19 夕颜 阅读(604) 评论(1) 编辑

<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

导航

统计

公告

昵称:夕颜
园龄:3年9个月
粉丝:0
关注:1

搜索

 
 

常用链接

我的标签

随笔分类

随笔档案

最新评论