实体或复合类型“FPSDB_newModel.Form_Attachment”不能在LINQ to Entities查询中构建 [英] The entity or complex type 'FPSDB_newModel.Form_Attachment' cannot be constructed in a LINQ to Entities query

查看:211
本文介绍了实体或复合类型“FPSDB_newModel.Form_Attachment”不能在LINQ to Entities查询中构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework DB的第一种方法。当我想从数据库中提取数据时,我使用方法 GetForm_AttachmentByAppID 。当我从我的代码调用这个方法时,我得到以下运行时错误:


  EntityFramework.SqlServer.dll中发生了类型System.NotSupportedException的异常,但在用户代码中未处理

附加信息:不能在LINQ to Entities查询中构造实体或复合类型FPSDB_newModel.Form_Attachment。


  public partial class Form_Attachment 
{
public int Form_AttachmentID {get;组; }
public Nullable< int> AttachmentCategoryID {get;组; }
public int ApplicationID {get;组; }
public string EmployeeID {get;组; }
public string DocumentName {get;组; }
public string DocumentSize {get;组; }
public Nullable< byte> RoleID {get;组; }
public string描述{get;组; }
public Nullable< bool> SelectionForExtRev {get;组; }

public virtual Application Application {get;组; }
public virtual AttachmentCategory AttachmentCategory {get;组; }
public virtual Employee Employee {get;组; }
public virtual Role Role {get;组; }
}

上述文件是自动生成的,代码存在于 FPSModel.cs ,而下面是我的代码来拉取数据

  public List< Form_Attachment> GetForm_AttachmentByAppID(int applicationID)
{
var context = new FPSDB_newEntities();
var data =(from f in context.Form_Attachment
其中

f.ApplicationID == applicationID

选择新Form_Attachment
{
Form_AttachmentID = f.Form_AttachmentID,
AttachmentCategoryID = f.AttachmentCategoryID,
EmployeeID = f.EmployeeID,
ApplicationID = f.ApplicationID,
DocumentName = f.DocumentName ,
DocumentSize = f.DocumentSize,
RoleID = f.RoleID,
描述= f.Description,
SelectionForExtRev = f.SelectionForExtRev
})ToList() ;
返回数据;
}

这里也是我的表格的数据库定义 Form_Attachments

  CREATE TABLE [dbo]。[Form_Attachment](
[Form_AttachmentID] [int] IDENTITY )NOT NULL,
[AttachmentCategoryID] [int] NULL,
[ApplicationID] [int] NOT NULL,
[EmployeeID] [varchar](10)NOT NULL,
[文件名] [nvarchar](500)NULL,
[DocumentSize] [nvarchar](50)NULL,
[RoleID] [tinyint] NULL,
[描述] [ntext] b $ b [SelectionForExtRev] [bit] NULL,
CONSTRAINT [PK_Form_AttachmentID] PRIMARY KEY CLUSTERED

[Form_AttachmentID] ASC
)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF, $ PR $ ON ON ON $ $ ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON ON $ $
$ b

我已经阅读了这个 Proj ection不应该被映射到实体
所以如果我创建一个类 Form_Attachments1 ,就像自动生成的 Form_Attachments 一样,它将成为一个数据转移对象DTO是一种正确的方法。

解决方案

您可以尝试以下方式:

  public List< Form_Attachment> GetForm_AttachmentByAppID(int applicationID)
{
var context = new FPSDB_newEntities();
var data =(from f in context.Form_Attachment
where

f.ApplicationID == applicationID

select f).ToList() ;
返回数据;
}

或此:

  data = context.Form_Attachment.Where(p => p.ApplicationID == applicationID).ToList(); 

我现在无法测试,但这应该可以工作。你不需要映射,因为'f'意味着你的'Form_Attachment'实体类已经。如果您使用像Form_AttachmentDto这样的DTO类,那么您需要像这样映射:

  public List< Form_AttachmentDto> ; GetForm_AttachmentByAppID(int applicationID)
{
var context = new FPSDB_newEntities();
var data =(from f in context.Form_Attachment
where

f.ApplicationID == applicationID

select f).Select(p => new Form_AttachmentDto()
{
// ...
//这里映射代码
// ..
}
) .ToList();
返回数据;
}


I am using Entity Framework DB first approach. When I want to pull the data from DB I use the method GetForm_AttachmentByAppID.I get the following run time error when I call this method from my code:

An exception of type 'System.NotSupportedException' occurred in     EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The entity or complex type 'FPSDB_newModel.Form_Attachment' cannot be constructed in a LINQ to Entities query.

public partial class Form_Attachment
{
public int Form_AttachmentID { get; set; }
public Nullable<int> AttachmentCategoryID { get; set; }
public int ApplicationID { get; set; }
public string EmployeeID { get; set; }
public string DocumentName { get; set; }
public string DocumentSize { get; set; }
public Nullable<byte> RoleID { get; set; }
public string Description { get; set; }
public Nullable<bool> SelectionForExtRev { get; set; }

public virtual Application Application { get; set; }
public virtual AttachmentCategory AttachmentCategory { get; set; }
public virtual Employee Employee { get; set; }
public virtual Role Role { get; set; }
}    

The above file is auto-generated and the code is present in FPSModel.cs while below is my code to pull data

public List<Form_Attachment> GetForm_AttachmentByAppID(int applicationID)
{
    var context = new FPSDB_newEntities();
    var data = (from f in context.Form_Attachment
                where
                (
                f.ApplicationID== applicationID
                )
                select new Form_Attachment
                {
                    Form_AttachmentID = f.Form_AttachmentID,
                    AttachmentCategoryID = f.AttachmentCategoryID,
                    EmployeeID = f.EmployeeID,
                    ApplicationID = f.ApplicationID,
                    DocumentName = f.DocumentName,
                    DocumentSize = f.DocumentSize,
                    RoleID = f.RoleID,
                    Description = f.Description,
                    SelectionForExtRev = f.SelectionForExtRev
                }).ToList();
    return data;
}

Also here is the DB definition of my table Form_Attachments

CREATE TABLE [dbo].[Form_Attachment](
[Form_AttachmentID] [int] IDENTITY(1,1) NOT NULL,
[AttachmentCategoryID] [int] NULL,
[ApplicationID] [int] NOT NULL,
[EmployeeID] [varchar](10) NOT NULL,
[DocumentName] [nvarchar](500) NULL,
[DocumentSize] [nvarchar](50) NULL,
[RoleID] [tinyint] NULL,
[Description] [ntext] NULL,
[SelectionForExtRev] [bit] NULL,
 CONSTRAINT [PK_Form_AttachmentID] PRIMARY KEY CLUSTERED 
(
[Form_AttachmentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

I have read this The Projection should not be on to a mapped entity So if i make a class Form_Attachments1 just like auto-generated Form_Attachments so will it be a Data Transfer object DTO and is it a right approach.

解决方案

You can try this:

public List<Form_Attachment> GetForm_AttachmentByAppID(int applicationID)
{
    var context = new FPSDB_newEntities();
    var data = (from f in context.Form_Attachment
                where
                (
                f.ApplicationID== applicationID
                )
                select f).ToList();
    return data;
}

or this:

data = context.Form_Attachment.Where(p => p.ApplicationID == applicationID).ToList();

I can't test it now, but this should work. You don't need to map because 'f' means your 'Form_Attachment' entity class already. If you were using a DTO class like 'Form_AttachmentDto', then you would need to map it like this:

public List<Form_AttachmentDto> GetForm_AttachmentByAppID(int applicationID)
    {
        var context = new FPSDB_newEntities();
        var data = (from f in context.Form_Attachment
                    where
                    (
                    f.ApplicationID== applicationID
                    )
                    select f).Select(p=>new Form_AttachmentDto() 
                        { 
                        //...
                        // here comes the mapping code
                        //..
                        }
                    ).ToList();
        return data;
    }

这篇关于实体或复合类型“FPSDB_newModel.Form_Attachment”不能在LINQ to Entities查询中构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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