私はEntity Framework DBの最初のアプローチを使用しています。エンティティまたは複合型 'FPSDB_newModel.Form_Attachment'は、LINQ to Entitiesクエリでは構築できません
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; }
}
:私はDBからデータを取得したい場合は、私は私のコードからこのメソッドを呼び出すときに、次の実行時エラーが発生します.I方法
GetForm_AttachmentByAppIDを使用します
上記のファイルは自動生成され、コードは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;
}
はまた、ここに私のテーブルのDB定義です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]
私は私がクラスを作るもしそうなら、このThe Projection should not be on to a mapped entity を読んだだけで、自動生成Form_Attachmentsそう意志のようなForm_Attachments1それはデータ転送オブジェクトDTOであり、正しいアプローチです。
data = context.Form_Attachment.Where(p => p.ApplicationID == applicationID).ToList();
が、私は今それをテストすることはできませんが、これは動作するはずです:
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;
}
またはこれを:
完璧なソリューション!おかげです。 – shomaail
私は助けてうれしいです。 –
私が気づいたことのある他の質問については、DTOクラスを使用する方が良い方法です。私は常にデータアクセス層とプレゼンテーション層の中間レベル(ビジネス層)としてDTOクラスを使用します。このアプローチは、検証、ロギング、認可などのデータの干渉を柔軟にしてくれます。もちろん、もっと多くの利点があります。良いコーディング:) –