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

  • Question

  • User81789783 posted

    i m facing below error

    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 'CallCenterModel.TBL_CustomerMaster' cannot be constructed in a LINQ to Entities query.

    i have below function  where error /exception occured and TBL_CustomerMaster is auto generated class

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Manual changes to this file may cause unexpected behavior in your application.
    //     Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace CallCenterCRM
    {
        using System;
        using System.Collections.Generic;
        
        public partial class TBL_CustomerMaster
        {
            public int CustomerId { get; set; }
            public string Cus_Name { get; set; }
            public System.DateTime ? Cus_RegDate { get; set; }
            public System.DateTime ?  Cus_RegExpDate { get; set; }
            public string Cust_Address { get; set; }
            public string Cus_Email { get; set; }
        }
    }
    

      public List<TBL_CustomerMaster> GetCustomerByID(int Customerid)
        {
    
            var results = (from p in db.TBL_CustomerMaster
                      
                           where p.CustomerId == Customerid
    
                           select new TBL_CustomerMaster
    
                           {
                               Cus_Name = p.Cus_Name,
                               Cust_Address = p.Cust_Address,
                               Cus_Email = p.Cus_Email 
                               
                               
                               //  prefix = pi.prefix,
    
    
    
                           }).ToList();
            return results;
    
        }

    please note down i m using EF 6 and MVC 5

    Tuesday, February 13, 2018 4:06 PM

Answers

  • User81789783 posted

    i make own class for this system generated model class and use this and its working fine .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using System.Linq;
    using System.ComponentModel.DataAnnotations;
    using System.Web;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;

    namespace CallCenterCRM.Models
    {
    public class CustomerModel
    {

    [HiddenInput(DisplayValue = true)]
    [Key]

    public int CustomerId { get; set; }
    public string Cus_Name { get; set; }
    public System.DateTime? Cus_RegDate { get; set; }
    public System.DateTime? Cus_RegExpDate { get; set; }
    public string Cust_Address { get; set; }
    public string Cus_Email { get; set; }
    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 14, 2018 7:00 AM

All replies

  • User765422875 posted

    Entity Framework doesn't allow you to project the results of a query into a mapped entity. You can either do what you've done and use a DTO which doesn't inherit from the mapped entity or project into an anonymous type.

    Tuesday, February 13, 2018 8:50 PM
  • User81789783 posted

    i make own class for this system generated model class and use this and its working fine .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using System.Linq;
    using System.ComponentModel.DataAnnotations;
    using System.Web;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;

    namespace CallCenterCRM.Models
    {
    public class CustomerModel
    {

    [HiddenInput(DisplayValue = true)]
    [Key]

    public int CustomerId { get; set; }
    public string Cus_Name { get; set; }
    public System.DateTime? Cus_RegDate { get; set; }
    public System.DateTime? Cus_RegExpDate { get; set; }
    public string Cust_Address { get; set; }
    public string Cus_Email { get; set; }
    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 14, 2018 7:00 AM