System.Data.Entity.dll中发生类型'System.NotSupportedException'的异常,但未在用户代码中处理附加信息:LINQ to Entities无法识别方法'System.Collections.Generic.List`1 [ProjectServices.Sale_Detail_D [英] An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code Additional information: LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[ProjectServices.Sale_Detail_D

查看:56
本文介绍了System.Data.Entity.dll中发生类型'System.NotSupportedException'的异常,但未在用户代码中处理附加信息:LINQ to Entities无法识别方法'System.Collections.Generic.List`1 [ProjectServices.Sale_Detail_D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    *** Mysql数据库与Entity框架一起使用是5.0

   *** Mysql database is used with Entity framework is 5.0

public List< Sale_Head_DTO> ProfitReport()

  &NBSP; &NBSP; &NBSP; {

$
  &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;列表与LT; Sale_Head_DTO> selected = null;



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;使用(medical_storeEntities db = new medical_storeEntities())

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

$
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   //试试
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   // {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;选择=(从在db.sale_header.Include(QUOT; sale_detail")

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;    //其中a.sale_header_id == 3875

              &NBSP;&NBSP;&NBSP;选择新Sale_Head_DTO()

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; {

                   sale_header_id = a。 sale_header_id,

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cus_name =一个。 cus_name,

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; SALE_DATE =一个。 sale_date,



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Sale_Detail_DTO =(从s中a.sale_detail选择新Sale_Detail_DTO()&NBSP;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;        {sale_detail_id = s.sale_detail_id})。ToList()



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  })。ToList();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;选择退货;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   //}

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // catch(例外xe)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // { 

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //}



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //返回选中;



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }

public List<Sale_Head_DTO> ProfitReport()
        {

          
            List<Sale_Head_DTO> selected = null;

            using (medical_storeEntities db = new medical_storeEntities())
            {

               // try
               // {
                    selected = (from a in db.sale_header.Include("sale_detail")
                               // where a.sale_header_id==3875
                                select new Sale_Head_DTO()
                               {
                                   sale_header_id = a.sale_header_id,
                                   cus_name = a.cus_name,
                                    sale_date = a.sale_date,

                                   Sale_Detail_DTO = (from s in a.sale_detail select new Sale_Detail_DTO() 
                                   { sale_detail_id = s.sale_detail_id}).ToList()

                               }).ToList();
                    return selected;
               // }
                //catch (Exception xe)
                //{ 
                //}

                //return selected;

            }
        }

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web ;
$
使用System.Runtime.Serialization;
$
命名空间ProjectServices

{

  &NBSP; [DataContract]

  &NBSP;公共课Sale_Head_DTO

  &NBSP; {

$
  &NBSP; &NBSP; &NBSP;公开Sale_Head_DTO()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.Sale_Detail_DTO = new HashSet< Sale_Detail_DTO>();

  &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; [DataMember]

  &NBSP; &NBSP; &NBSP; public int sale_header_id {get;组; }¥b $ b  &NBSP; &NBSP; &NBSP; [DataMember]

  &NBSP; &NBSP; &NBSP; public Nullable< System.DateTime> sale_date {get;组; }¥b $ b  &NBSP; &NBSP; &NBSP; [DataMember]

  &NBSP; &NBSP; &NBSP; public string cus_name {get;组; }¥b $ b  &NBSP; &NBSP; &NBSP; [DataMember]

  &NBSP; &NBSP; &NBSP; public virtual ICollection< Sale_Detail_DTO> Sale_Detail_DTO {get;组; }¥b $ b  &NBSP; }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace ProjectServices
{
    [DataContract]
    public class Sale_Head_DTO
    {

        public Sale_Head_DTO()
        {
            this.Sale_Detail_DTO = new HashSet<Sale_Detail_DTO>();
        }
        [DataMember]
        public int sale_header_id { get; set; }
        [DataMember]
        public Nullable<System.DateTime> sale_date { get; set; }
        [DataMember]
        public string cus_name { get; set; }
        [DataMember]
        public virtual ICollection<Sale_Detail_DTO> Sale_Detail_DTO { get; set; }
    }
}

推荐答案




首先,你的集合命名约定应该有多个命名约定,表明大于1,Master =只有1,Masters =大于1.有人看着代码应该能够通过查看对象的名称来查看什么是集合以及什么是
而不是集合。

First off,  your naming convention for a collection should have a plural naming convention showing that the is more than 1, Master = only 1 and Masters = more than 1. Someone looking at the code should be able to see what is a collection and what is not a collection by just looking at the name of the object.

其次,Sale_Head_DTO应该在其自己的classlib项目中定义,名为Entities 。所有需要使用DTO的项目都应该设置对实体的项目参考,以便他们知道DTO的内容。

Secondly, the Sale_Head_DTO should be defined in its own classlib project called Entities. And all projects needing to use the DTO should set project reference to Entities so that they know what the DTO is about.

第三,选择新的Sale_Head_DTO(),是的,你在Linq投影中创建了一个新对象,但你没有做任何事情,只需


        public virtual的ICollection< Sale_Detail_DTO> Sale_Detail_DTO&NBSP; &NBSP;在
Sale_Head_DTO 内将其初始化为甚至使其成为列表< T>
内的对象 Sale_Head_DTO()。

Thirdly, select new Sale_Head_DTO(), yeah you created a new object in a Linq projection,  but you did nothing with
        public virtual ICollection<Sale_Detail_DTO> Sale_Detail_DTO   within Sale_Head_DTO initializing it to even make it a List<T> object within Sale_Head_DTO().

第四,您还没有给出完整的错误消息,甚至不知道异常是什么,因为您允许通过将异常消息放入主题行来切断异常消息。

Fourthly , you have not given the full error message to even know what the exception is about, since you allowed the exception message to be cutoff by putting it in the subject line.

最后,EF问题在EF论坛上解决。

Lastly, EF issues are addressed at the EF forum.

https: //social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework


这篇关于System.Data.Entity.dll中发生类型'System.NotSupportedException'的异常,但未在用户代码中处理附加信息:LINQ to Entities无法识别方法'System.Collections.Generic.List`1 [ProjectServices.Sale_Detail_D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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