Solved

Cannot convert lambda expression to type 'string' because it is not a delegate type

Posted on 2014-04-04
3
4,027 Views
Last Modified: 2014-04-07
I am creating an MVC4 application using TDD and EF v5.  I am creating my model and allowing EF to create the database.  Because I am using TDD I am using a Repository Pattern.  The problem I am having is I can't use a lamda expression to include another table.  The lamda works for OrderBy or Where but not Include.

Here is how everything is setup:
AppCatalog_Models (as created by EF) - this is the table that I am having trouble with:
namespace AppCatalog.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class AppCatalog_Database
    {
        public AppCatalog_Database()
        {
            this.AppCatalog_DBConnection = new HashSet<AppCatalog_DBConnection>();
            this.AppCatalog_PIIData = new HashSet<AppCatalog_PIIData>();
            this.AppCatalog_Application = new HashSet<AppCatalog_Application>();
        }
    
        public int databaseID { get; set; }
        public string databaseName { get; set; }
        public string databaseDescription { get; set; }
        public int databaseServerTypeID { get; set; }
    
        public virtual AppCatalog_ServerType AppCatalog_ServerType { get; set; }
        public virtual ICollection<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
        public virtual ICollection<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
        public virtual ICollection<AppCatalog_Application> AppCatalog_Application { get; set; }
    }
}

Open in new window


The Interface that I created:
IAppCatalogContainger
namespace AppCatalog.Models.Repository
{
    public interface IAppCatalogContainer 
    {
        IDbSet<AppCatalog_Database> AppCatalog_Database { get; set; }
        IDbSet<AppCatalog_ServerType> AppCatalog_ServerType { get; set; }
        IDbSet<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
        IDbSet<AppCatalog_Environment> AppCatalog_Environment { get; set; }
        IDbSet<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
        IDbSet<AppCatalog_Application> AppCatalog_Application { get; set; }
        IDbSet<AppCatalog_ApplicationTable> AppCatalog_ApplicationTable { get; set; }

        int SaveChanges();
        DbEntityEntry Entry(Object entity);
        void Dispose();
     }
}

Open in new window


Implementation of the Interface Repository:
IAppCatalogContainerRepository
using System;
using System.Collections.Generic;

namespace AppCatalog.Models.Repository
{
    public interface IAppCatalogContainerRepository
    {
        int SaveChanges();

        //******************************************************************************************************************
        // AppCatalog_Database methods
        //******************************************************************************************************************
        void CreateNewDatabase(AppCatalog_Database databaseToCreate);
        AppCatalog_Database GetDatabaseByID(int id);
        IEnumerable<AppCatalog_Database> GetAllDatabases();
        bool IsDatabaseNameUnique(int serverType, string databaseName, int databaseID);
        void SaveChanges(AppCatalog_Database dataToUpdate);

        //******************************************************************************************************************
        // AppCatalog_ServerType methods
        //******************************************************************************************************************
        IEnumerable<AppCatalog_ServerType> GetAllServerTypes();

        //******************************************************************************************************************
        // AppCatalog_Environment methods
        //******************************************************************************************************************
        IEnumerable<AppCatalog_Environment> GetAllEnvironments();

        //******************************************************************************************************************
        // AppCatalog_DBConnections methods
        //******************************************************************************************************************
        void CreateNewDBConnection(AppCatalog_DBConnection dbconnectionToCreate);
        AppCatalog_DBConnection GetDBConnectionByID(int id);
        IEnumerable<AppCatalog_DBConnection> GetAllDBConnectionsForDatabase(int databaseID);
        bool IsUserNameUnique(int environmentID, string userName, int dbconnectionID);
        void SaveChanges(AppCatalog_DBConnection dataToUpdate);

    }
}

Open in new window


The EF implementation of the Repository Interface:
EF_AppCatalogContainerManagerRepository
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Data;

namespace AppCatalog.Models.Repository
{
    public class EF_AppCatalogContainerManagerRepository : AppCatalog.Models.Repository.IAppCatalogContainerRepository
    {
        private AppCatalogContainer _db = new AppCatalogContainer();

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }


        //******************************************************************************************************************
        // AppCatalog_Database methods
        //******************************************************************************************************************
        public AppCatalog_Database GetDatabaseByID(int id)
        {
            return _db.AppCatalog_Database.FirstOrDefault(d => d.databaseID == id);
        }

        public IEnumerable<AppCatalog_Database> GetAllDatabases()
        {
            var appcatalog_database = _db.AppCatalog_Database
                                        .Include(a => a.AppCatalog_ServerType)
                                        .OrderBy(a => a.AppCatalog_ServerType.servertypeName).ThenBy(a => a.databaseName);
            //return appcatalog_database.ToList();
            return _db.AppCatalog_Database.ToList();
        }

        public void CreateNewDatabase(AppCatalog_Database databaseToCreate)
        {
            _db.AppCatalog_Database.Add(databaseToCreate);
            _db.SaveChanges();
        }

        public bool IsDatabaseNameUnique(int serverTypeID, string databaseName, int databaseID)
        {
            IEnumerable<AppCatalog_Database> dbRecords = _db.AppCatalog_Database.Where(a => a.databaseServerTypeID == serverTypeID 
                                                                                        && a.databaseName.ToUpper().Trim() == databaseName.ToUpper().Trim());
            int countRecords = dbRecords.Count();
            if (countRecords == 0)
                return true;            // Record not found for server type and database name

            // Expect 0 to 1 record to be returned.  If more than one record returned, throw an error
            if (countRecords > 1)
                throw new ValidationException("More than 1 record was returned");

            // Record found, verify record returned is not for the record being modified 
            if (databaseID != 0)
            {
                if (dbRecords.FirstOrDefault().databaseID == databaseID)
                {
                    return true;        // Found record being modified (server type and database name were not changed)
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public void SaveChanges(AppCatalog_Database dataToUpdate)
        {
            _db.Entry(dataToUpdate).State = EntityState.Modified;
            SaveChanges();
        }

        //******************************************************************************************************************
        // AppCatalog_ServerType methods
        //******************************************************************************************************************
        public IEnumerable<AppCatalog_ServerType> GetAllServerTypes()
        {
            return _db.AppCatalog_ServerType.ToList();
        }

        //******************************************************************************************************************
        // AppCatalog_Environment methods
        //******************************************************************************************************************
        public IEnumerable<AppCatalog_Environment> GetAllEnvironments()
        {
            return _db.AppCatalog_Environment.ToList();
        }

        //******************************************************************************************************************
        // AppCatalog_DBConnection methods
        //******************************************************************************************************************
        public AppCatalog_DBConnection GetDBConnectionByID(int id)
        {
            return _db.AppCatalog_DBConnection.FirstOrDefault(d => d.dbconnectionID == id);
        }

        public IEnumerable<AppCatalog_DBConnection> GetAllDBConnectionsForDatabase(int databaseID)
        {
            return _db.AppCatalog_DBConnection.Where(d => d.dbconnectionDatabaseID == databaseID).ToList();
        }

        public void CreateNewDBConnection(AppCatalog_DBConnection dbconnectionToCreate)
        {
            _db.AppCatalog_DBConnection.Add(dbconnectionToCreate);
            _db.SaveChanges();
        }

        public bool IsUserNameUnique(int environmentID, string userName, int dbconnectionID)
        {
            IEnumerable<AppCatalog_DBConnection> dbRecords = _db.AppCatalog_DBConnection.Where(a => a.dbconnectionEnvironmentID == environmentID
                                                                                        && a.dbconnectionUserName.ToUpper().Trim() == userName.ToUpper().Trim());
            int countRecords = dbRecords.Count();
            if (countRecords == 0)
                return true;            // Record not found for environment and user name

            // Expect 0 to 1 record to be returned.  If more than one record returned, throw an error
            if (countRecords > 1)
                throw new ValidationException("More than 1 record was returned");

            // Record found, verify record returned is not for the record being modified 
            if (dbconnectionID != 0)
            {
                if (dbRecords.FirstOrDefault().dbconnectionID == dbconnectionID)
                {
                    return true;        // Found record being modified (environment and user name were not changed)
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public void SaveChanges(AppCatalog_DBConnection dataToUpdate)
        {
            _db.Entry(dataToUpdate).State = EntityState.Modified;
            SaveChanges();
        }

    }
}

Open in new window


I am having trouble with the following routine in the EF_AppCatalogContainerManagerRepository:
        public IEnumerable<AppCatalog_Database> GetAllDatabases()
        {
            var appcatalog_database = _db.AppCatalog_Database
                                        .Include(a => a.AppCatalog_ServerType)
                                        .OrderBy(a => a.AppCatalog_ServerType.servertypeName).ThenBy(a => a.databaseName);
            //return appcatalog_database.ToList();
            return _db.AppCatalog_Database.ToList();
        }

Open in new window


I am getting the "Cannot convert lambda expression to type 'string' because it is not a delegate type" error on the .Include(a => a.AppCatalog_ServerType).  Intellisense doesn't show AppCatalog_ServerType as being valid yet it works in the OrderBy.

What changes do I need to make in order to get the Include to load the AppCatalog_ServerType table when the AppCatalog_Database table is loaded?  Any help is greatly appreciated!
0
Comment
Question by:dyarosh
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39979537
Make sure that you have included the following using statement.

using System.Data.Entity;

See this link for examples
DbExtensions.Include<T, TProperty> Method (IQueryable<T>, Expression<Func<T, TProperty>>)
0
 

Author Closing Comment

by:dyarosh
ID: 39982834
Awesome!  Thank you!!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39982904
Not a problem, glad I was able to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question