• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4315
  • Last Modified:

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

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
dyarosh
Asked:
dyarosh
  • 2
1 Solution
 
Fernando SotoRetiredCommented:
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
 
dyaroshAuthor Commented:
Awesome!  Thank you!!
0
 
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now