0

Here is the class I'm trying to create.

I get the error 'DbMainContext does not contain a definition for tName' It's the 3rd line under the using statement below.

I don't want to have to keep recoding the query in a lot of places, so it seemed like a helper method would keep the code cleaner.

public class DbHelper
    {
        static string GetValueFromDatabase(DatabaseData dbData)
        {
            string tName = dbData.TableName;

            using (DbMainContext db = new DbMainContext())
            {
                var query =
                   (from t in db.tName
                    where t.ID == 1198
                    select t.LastName).Single<String>();

                return query;

            }
        }
    }


    public class DatabaseData : IDatabaseData
    {
        public string TableName { get; set; }
        public string ColumnName { get; set; }
        public string WhereClause { get; set; }
        public int WhereId { get; set; }
    }

    public interface IDatabaseData
    {
        public string TableName { get; set; }
        public string ColumnName { get; set; }
        public string WhereClause { get; set; }
        public int WhereId { get; set; }
    }
}

1 Answers1

0

You're perhaps looking for something like How to generalise access to DbSet members of a DbContext?.

You can't use db."Users", so you can't use db.tName either. You could use reflection for that, but you don't want that.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272