【问题标题】:The entity or complex type 'FPSDB_newModel.Form_Attachment' cannot be constructed in a LINQ to Entities query无法在 LINQ to Entities 查询中构造实体或复杂类型“FPSDB_newModel.Form_Attachment”
【发布时间】:2017-05-31 07:48:09
【问题描述】:

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

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; }
}    

上面的文件是自动生成的,代码在FPSModel.cs中,下面是我提取数据的代码

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;
}

这也是我的表 Form_Attachments

的 DB 定义
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]

我读过这个The Projection should not be on to a mapped entity 因此,如果我创建一个类 Form_Attachments1 就像自动生成的 Form_Attachments 一样,那么它将是一个数据传输对象 DTO,这是一种正确的方法吗?

【问题讨论】:

    标签: asp.net entity-framework webforms entity dto


    【解决方案1】:

    你可以试试这个:

    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;
    }
    

    或者这个:

    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() 
                            { 
                            //...
                            // here comes the mapping code
                            //..
                            }
                        ).ToList();
            return data;
        }
    

    【讨论】:

    • 完美解决方案!谢谢亲爱的
    • 我很高兴能帮上忙。
    • 对于我刚刚注意到的其他问题,使用 DTO 类是一种更好的方法。我总是使用 DTO 类作为数据访问层和表示层之间的中间层(业务层)。这种方法为我提供了干扰数据的灵活性,例如验证、记录或授权等。当然还有很多好处,我不能在这里全部写出来。良好的编码:)
    • 非常感谢,如果你能回答stackoverflow.com/questions/41695514/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2015-02-06
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多