Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在linq to entities查询中构造实体或复杂类型“x”_C#_Entity Framework_Ef Code First_Linq To Entities_Csla - Fatal编程技术网

C# 无法在linq to entities查询中构造实体或复杂类型“x”

C# 无法在linq to entities查询中构造实体或复杂类型“x”,c#,entity-framework,ef-code-first,linq-to-entities,csla,C#,Entity Framework,Ef Code First,Linq To Entities,Csla,有谁能告诉我以下代码有什么问题,因为我在运行时在主题中遇到了此错误: 我的朋友: public class CustomerDal : ICustomerDal { public List<CustomerDto> Fetch() { using(var ctx = DbContextManager<CustomerContext>.GetManager("CustomerDB")) { var re

有谁能告诉我以下代码有什么问题,因为我在运行时在主题中遇到了此错误:

我的朋友:

public class CustomerDal : ICustomerDal
{
    public List<CustomerDto> Fetch()
    {
        using(var ctx = DbContextManager<CustomerContext>.GetManager("CustomerDB"))
        {
            var result = from r in ctx.DbContext.Customers 
                         select new CustomerDto
                         {
                             CustomerId = r.CustomerId,
                             Name = r.Name,
                             Email = r.Email
                         }

            return result.ToList();
        }
    }
该数据库是一个名为CustomerDB的LocalDb数据库,以及一个名为Customer的表,其中包含CustomerId、name和Email列

但我注意到,如果我使用匿名函数将DAL代码更改为以下代码,它将正常运行,但我仍然无法从数据库中获取任何数据:

    public List<CustomerDto> Fetch()
    {
        using(var ctx = DbContextManager<CustomerContext>.GetManager("CustomerDB"))
        {
            var result = (from r in ctx.DbContext.Customers 
                         select new 
                         {
                             CustomerId = r.CustomerId,
                             Name = r.Name,
                             Email = r.Email
                         }).ToList().Select(x => new CustomerDto{ CustomerId = x.CustomerId, Name = x.Name, Email = x.Email });

            return result.ToList();
        }
    }
我也在使用CSLA框架,但这在这件事上不会有任何区别

我看到论坛上也有类似的问题,但没有一个能100%回答我的问题,因为我使用的是DTO,我发现的所有问题从一开始都没有用到

任何帮助都将不胜感激

谢谢,
Peter

你可以让你的客户从客户那里获得

public class CustomerDto : Customer { }

public List<CustomerDto> Fetch(int categoryID)
{
    return (from p in db.Products
            select new CustomerDto()
            {
                CustomerId = r.CustomerId,
                Name = r.Name,
                Email = r.Email
            }).ToList();
}

问题主要在于linqtoentities不支持整个linq功能。如果您想像使用DTO一样进行操作,。。。您需要执行ToList,然后选择进入dto。对于复杂的情况也是如此,使用方法的机制,即使是linq通常支持的一些内置方法,也会导致与Noramly相同的错误,我想说的是,您的任意名称代码都会导致相同的错误。您是否也尝试过在ctx.DbContext.Customers select r中调试if from r.ToList结果为零?可能正是因为这个原因,我的措辞并不完全糟糕。顺便说一句,我刚才看到您在dto中使用[Key]?如果仍然抛出错误,是否已尝试删除该错误?据我所知,EF使用的关键注释可能会导致EF错误地解释CustomerTo是一个entity@Thomas,如果我取下[钥匙]-属性I在“return result.ToList”之前出现以下错误:在模型生成期间检测到一个或多个验证错误:CSLAEFCodeFirstTest.DalEf.CustomerDto::EntityType“CustomerDto”未定义键。定义此EntityType的键。Customers:EntityType:EntitySet“Customers”基于没有定义键的类型“CustomerTo”。我还意识到,在第一个示例中,我没有得到任何数据。它通过查询,但在“return result.ToList”上停止。我遗漏了什么?另外,我不想从客户那里继承,因为我想使用一个纯Dto,它类似于数据库中的表,只是一个数据载体。
    public List<CustomerDto> Fetch()
    {
        using(var ctx = DbContextManager<CustomerContext>.GetManager("CustomerDB"))
        {
            var result = (from r in ctx.DbContext.Customers 
                         select new 
                         {
                             CustomerId = r.CustomerId,
                             Name = r.Name,
                             Email = r.Email
                         }).ToList().Select(x => new CustomerDto{ CustomerId = x.CustomerId, Name = x.Name, Email = x.Email });

            return result.ToList();
        }
    }
public class CustomerDto : Customer { }

public List<CustomerDto> Fetch(int categoryID)
{
    return (from p in db.Products
            select new CustomerDto()
            {
                CustomerId = r.CustomerId,
                Name = r.Name,
                Email = r.Email
            }).ToList();
}