Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to process the type , because it has no known mapping to the value layer. C# models foreign key on primary key of same class

I'm trying to represent a folder stucture in a MSSQL DB I'm using folderID as a primary key with a nullable int of parentFolderID as a foreign key. My class in c# is:

public class FileFoldersDisplayClass
{
    public int folderID { get; set; }

    public string folderName { get; set; }

    public int? parentFolderID { get; set; }

    public int? contractorID { get; set; }

    public int? teamID { get; set; }

    public int? contractID { get; set; }

    public int? clientID { get; set; }

    public bool bureau { get; set; }

    public bool teamManager { get; set; }

    public bool staff { get; set; }

    public bool? secure { get; set; }

    public List<FileFoldersDisplayClass> childFolders { get; set; }
}

FileFolderDisplayModel has a property of List(FileFolderDisplayModel) which I'm trying to populate with the following function:

 List<FileFoldersDisplayClass> ITeamsRepository.GetTopLevelFolders(int? teamID, int? clientID, int? contractorID, int staffID, bool secure)
    {
        CMS3Context _db = new CMS3Context();
        try
        {
            var childFolders = new List<FileFoldersDisplayClass>();
            var folders = from f in _db.FileFolders
                          where (((f.teamID == teamID && f.teamID != null) || (f.contractorID == contractorID && f.contractID != null) || (f.clientID == clientID && f.clientID != null)) && (f.secure ?? false == secure)) && f.parentFolderID == null
                          select new FileFoldersDisplayClass()
                          {
                              folderID = f.folderID,
                              folderName = f.folderName,
                              parentFolderID = f.parentFolderID,
                              contractorID = f.contractorID,
                              teamID = f.teamID,
                              contractID = f.contractID,
                              bureau = f.bureau,
                              teamManager = f.teamManager,
                              staff = f.staff,
                              secure = f.secure,
                              childFolders = (from cF in _db.FileFolders
                                             where cF.parentFolderID == f.folderID
                                             select new FileFoldersDisplayClass()
                                             {
                                                 folderID = f.folderID,
                                                 folderName = f.folderName,
                                                 parentFolderID = f.parentFolderID,
                                                 contractorID = f.contractorID,
                                                 teamID = f.teamID,
                                                 contractID = f.contractID,
                                                 bureau = f.bureau,
                                                 teamManager = f.teamManager,
                                                 staff = f.staff,
                                                 secure = f.secure,
                                                 childFolders = childFolders
                                             }).ToList()
                          };





            return folders.ToList();
        }
        catch(Exception e)
        {
            var d = e;
            return new List<FileFoldersDisplayClass>();
        }

    }

Here is my CREATE Script for my DBTable:

 CREATE TABLE [dbo].[tblFileFolders](
    [folderID] [int] IDENTITY(1,1) NOT NULL,
    [folderName] [nvarchar](50) NULL,
    [parentFolderID] [int] NULL,
    [contractorID] [int] NULL,
    [teamID] [int] NULL,
    [contractID] [int] NULL,
    [createdBy] [int] NULL,
    [bureau] [bit] NOT NULL,
    [teamManager] [bit] NOT NULL,
    [staff] [bit] NOT NULL,
    [clientID] [int] NULL,
    [secure] [bit] NULL
  ) ON [PRIMARY]

I'm getting exception:

"Unable to process the type '[]', because it has no known mapping to the value layer."

I am using Linq and Entity Framework.

Any ideas?

Edit - fixed:

So fixed it now, here was the solution that worked for me for anyone who is interested:

    List<FileFoldersDisplayClass> ITeamsRepository.GetFolders(int? teamID, int? clientID, int? contractorID, int staffID, bool secure)
    {
        CMS3Context _db = new CMS3Context();
        var topFolders = new List<FileFoldersDisplayClass>();
        var childFolders = new List<FileFoldersDisplayClass>();
        try {
              var folders = _db.FileFolders.Where(f => (f.teamID == teamID && f.teamID != null) || (f.contractorID == contractorID && f.contractID != null) || (f.clientID == clientID && f.clientID != null) && (f.secure ?? false == secure)).Select(f => new FileFoldersDisplayClass() {
                folderID = f.folderID,
                folderName = f.folderName,
                parentFolderID = f.parentFolderID,
                contractorID = f.contractorID,
                teamID = f.teamID,
                contractID = f.contractID,
                bureau = f.bureau,
                teamManager = f.teamManager,
                staff = f.staff,
                secure = f.secure
        });
            topFolders = folders.ToList();
            foreach (var f in topFolders)
            {

                childFolders = ((ITeamsRepository)this).GetChildFolders(teamID, clientID, contractorID, staffID, secure, f.folderID);
                f.childFolders = childFolders.AsEnumerable();
            };
            return folders.ToList();
        }
        catch
        {
            return new List<FileFoldersDisplayClass>();
        }

    }

 List<FileFoldersDisplayClass> ITeamsRepository.GetChildFolders(int? teamID, int? clientID, int? contractorID, int staffID, bool secure, int parentFolderID)
    {
        try
        {
            CMS3Context _db = new CMS3Context();
            var topFolders = new List<FileFoldersDisplayClass>();
            var childFolders = new List<FileFoldersDisplayClass>();
            var folders = _db.FileFolders.Where(f => (f.teamID == teamID && f.teamID != null) || (f.contractorID == contractorID && f.contractID != null) || (f.clientID == clientID && f.clientID != null) && (f.secure ?? false == secure) && (f.parentFolderID == parentFolderID)).Select(f => new FileFoldersDisplayClass()
            {
                folderID = f.folderID,
                folderName = f.folderName,
                parentFolderID = f.parentFolderID,
                contractorID = f.contractorID,
                teamID = f.teamID,
                contractID = f.contractID,
                bureau = f.bureau,
                teamManager = f.teamManager,
                staff = f.staff,
                secure = f.secure
            });
            topFolders = folders.ToList();
            foreach (var f in topFolders)
            {
                childFolders = ((ITeamsRepository)this).GetChildFolders(teamID, clientID, contractorID, staffID, secure, f.folderID);
                f.childFolders = childFolders.AsEnumerable();
            };
            return folders.ToList();
        }
        catch
        {
            return new List<FileFoldersDisplayClass>();
        }
    }

And I changed my model to:

public class FileFoldersDisplayClass
{
    public int folderID { get; set; }

    public string folderName { get; set; }

    public int? parentFolderID { get; set; }

    public int? contractorID { get; set; }

    public int? teamID { get; set; }

    public int? contractID { get; set; }

    public int? clientID { get; set; }

    public bool bureau { get; set; }

    public bool teamManager { get; set; }

    public bool staff { get; set; }

    public bool? secure { get; set; }

    public IEnumerable<FileFoldersDisplayClass> childFolders { get; set; }
}

And setup navigational properties in my DBTable models:

  public class tblFileUploads
{
    [Key]
    public int fileUploadID { get; set; }
    public string fileName { get; set; }
    public string contentType { get; set; }
    public byte[] fileData { get; set; }
    public int fileTypeID { get; set; }
    public string fileDescription { get; set; }
    public string fileTitle { get; set; }
    public string icon { get; set; }
    public double? totalExpenses { get; set; }
    public int? folderID { get; set; }
    [ForeignKey("fileTypeID")]
    public virtual tblFileUploadTypes FileType { get; set; }
    [ForeignKey("folderID")]
    public virtual tblFileFolders Folder { get; set; }
}

 public class tblFileFolderChildren
{
    [Key]
    public int childParentID { get; set; }
    public int folderID { get; set; }
    public int parentID { get; set; }
    [ForeignKey("parentFolderID")]
    public virtual tblFileFolders folder { get; set; }
}
like image 945
James Batten Avatar asked Nov 19 '25 09:11

James Batten


1 Answers

I had this same error, on a very different set of LINQ queries. Couldn't get to the bottom of it.

It's not much, but if it helps anyone:

The way I fixed it was just rewriting my LINQ queries to work in a different way. I was able to combine two into something simpler.

Hope that helps.

like image 159
MGOwen Avatar answered Nov 20 '25 22:11

MGOwen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!