本文介绍了[MyClass的在查询实体类型的显式建筑是不允许的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之类的标题说,我有以下异常:

Like the title says, I have the following exception:

描述:事件code:3005事件
  消息:未处理的异常
  发生。异常信息:
      异常类型:NotSupportedException异常
      异常消息:实体类型的显式建筑
  Company.Project.Core.Domain.Friend
  在查询中是不允许的。

我使用LINQ to SQL和在我的DataContext以下code:

I am using LINQ to SQL and have the following code in my datacontext:

var friends2 = (
    from f in dc.Friends
    where f.MyFriendsAccountId == accountId
    where f.AccountId != accountId
    select new 
    {
        f.FriendId,
        AccountId = f.MyFriendsAccountId,
        MyFriendsAccountId = f.AccountId, 
        f.CreateDate,
        f.Timestamp
    }).Distinct();

result.AddRange(
    from o in friends2
    select new Friend()
    {
        FriendId = o.FriendId, 
        AccountId = o.AccountId, 
        CreateDate = o.CreateDate, 
        MyFriendsAccountId = o.MyFriendsAccountId, 
        Timestamp = o.Timestamp
    });

最后code块引发错误,我pretty确保它是这句话
这是罪魁祸首:

the final code block is throwing the error and I am pretty sure it is this statementthat is the culprit:

.Select( o => **new Friend**

我应该如何进行返工我的code,以避免这个错误?

How should I be reworking my code to avoid this error?

推荐答案

实体是不能使用LINQ查询来创建数据上下文的一部分。这是C#团队的一个深思熟虑的设计决策。由于实体newed了(手动)在选择语句,这将意味着他们不是由的DataContext 这可能混淆开发人员。在另一方面,当在DataContext会自动的插上提交的那些newed起来的实体,这将被混淆为好。剩下的唯一选择是传达给开发商,这不是一个好主意的事,那就是你看到什么情况发生。

Entities that are part of the data context can not be created using a LINQ query. This is a well thought design decision of the C# team. Because the entities are newed up (manually) in the Select statement, this would mean that they are not tracked by the DataContext and this can confuse developers. On the other hand, when the DataContext would automatically insert on submit those newed up entities, this would be confusing as well. The only option left was communicating to the developers that this is not such a good idea to do, and that is what you saw happening.

这篇关于[MyClass的在查询实体类型的显式建筑是不允许的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:58