none
The type 'X' is not mapped as a Table. RRS feed

  • Question

  • Hi,

    I have ADO.NET Entity Data Model for my tables and if I use it directly it works fine.

    I wanted to write a wrapper, so that I don't have to repeat the same code again. Ended up writing some DataContext Extension methods.

    So, have something like this to get Data from the Table.

    public static TEntity Get<TEntity>(this DataContext dataContext, object id, string primaryKeyName) 
                where TEntity : class, new()
            {
                if (id == null)
                    return new TEntity();
    
                var table = dataContext.GetTable<TEntity>();
                return table.Single(DynamicGet<TEntity>(primaryKeyName, id));
            }

    I call this method something like this

    System.Data.IDbConnection conn = new System.Data.SqlClient.SqlConnection("ConnectionString");
    
    conn.Open();
    dataContext = new DataContext(conn);
    dataContext.Get<X>(email, "id");

    When I call the above method I get this error: The type 'X' is not mapped as a Table.

    X is coming from the model that was created by ADO.NET and unable to figure out why it's throwing this exception.

    Any ideas ?

    Really appreciate.

    Harsimrat

    Thursday, December 19, 2013 6:28 PM

Answers

  • Hello,

    I recommend to use the Entity Framework and change the Get() method like below:

     public static IList<TEntity> Get<TEntity>(this DbContext db, Expression<Func<TEntity, bool>> query, string OrderBy, int from, int to) where TEntity : class,new()
            {
                DbSet<TEntity> entityDbSet = db.Set<TEntity>();
    
                IList<TEntity> list = null;
    
                if (to == 0)
                    to = 100;
    
                if (from > to)
                    from = 0;
    
                if (query == null)
                {
                    list = entityDbSet.OrderBy<TEntity>(OrderBy).Skip(from).Take(to - from).Cast<TEntity>().ToList();
                }
                else
                {
                    list = entityDbSet.Where(query).OrderBy<TEntity>(OrderBy).Skip(from).Take(to - from).Cast<TEntity>().ToList();
                }
                return list;
            }

    To add the OrderBy clause is because the method 'Skip' is only supported for sorted input in LINQ to Entities, it should be include an OrderBy before Skip.

    Since you want to query data dynamicly, we need to download the LINQ Dynamic Query Library from link below:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    We call it like below:

     using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
                {
                    var result = db.Get<Order>(order => order.OrderID == 1, "OrderID", 0, 1);
                }
    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by thukralz Tuesday, December 24, 2013 8:23 AM
    Tuesday, December 24, 2013 7:40 AM
    Moderator

All replies

  • Hello,

    >>X is coming from the model that was created by ADO.NET

    Is X an entity class or not? If it is, could you please share that how you have defined the X entity class?

    What methods GetTable and DynamicGet are used for?

    We can hardly know what is problem with part codes.

    If it is possible, you can upload your program to:

    https://skydrive.live.com

    We can download it to test it and if you are using database model, please share the schema.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 20, 2013 6:30 AM
    Moderator
  • X is an entity class and GetTable is coming from System.Data.Linq namespace.

    Code for DynamicGet

    private static Expression<Func<TEntity, bool>> DynamicGet<TEntity>(string primaryKeyName, object id)
            {
                var e = Expression.Parameter(typeof(TEntity), "e");
                var propInfo = GetCaseInsensitivePropertyInfo<TEntity>(primaryKeyName);
                var m = Expression.MakeMemberAccess(e, propInfo);
                var c = Expression.Constant(id);
                var b = Expression.Equal(m, c);
                var lambda = Expression.Lambda<Func<TEntity, bool>>(b, e);
                return lambda;
            }

    Also, all my functions and causing the same issue:

    This is another function I tried:

    public static IList<TEntity> Get<TEntity>(this DataContext dataContext, DataLoadOptions options, Expression<Func<TEntity, bool>> query, 
                int from, int to) where TEntity : class, new()
            {
                IList<TEntity> list = null;
    
                if (to == 0)
                    to = 100;
    
                if (from > to)
                    from = 0;
    
                if (options != null)
                    dataContext.LoadOptions = options;
    
                if (query == null)
                    list = dataContext.GetTable<TEntity>().Skip(from).Take(to - from).ToList();
    
                list = dataContext.GetTable<TEntity>().Where(query).Skip(from).Take(to - from).ToList();            
    
                return list;
            }

    Call the above method is also causing the same issue:

    return dataContext.Get<X>(null, a => a.Email == email, 0, 1)[0];

    These are the namespaces to make this code work:

        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Collections.Specialized;
        using System.ComponentModel;
        using System.Data.Linq;
        using System.Linq;
        using System.Linq.Expressions;
        using System.Reflection;


    X is a direct mapping created by ADO.NET. This is what the table mapping looks like:

    • Edited by thukralz Friday, December 20, 2013 9:35 AM Added table Mapping Screenshot
    Friday, December 20, 2013 6:50 AM
  • Hello,

    Sorry for being late.

    >>GetCaseInsensitivePropertyInfo

    How is this method defined? It seems that there are lots of methods which are use-defined.

    You are appreciated that if you could upload your program to https://skydrive.live.com  so that we can know how it will run more clearly.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 23, 2013 9:36 AM
    Moderator
  • Hello,

    My only reluctance is that there is lot of proprietary code and policies around putting on public cloud.

    That being said; If you can please try it with this function:

    public static IList<TEntity> Get<TEntity>(this DataContext dataContext, DataLoadOptions options, Expression<Func<TEntity, bool>> query, 
                int from, int to) where TEntity : class, new()
            {
                IList<TEntity> list = null;
    
                if (to == 0)
                    to = 100;
    
                if (from > to)
                    from = 0;
    
                if (options != null)
                    dataContext.LoadOptions = options;
    
                if (query == null)
                    list = dataContext.GetTable<TEntity>().Skip(from).Take(to - from).ToList();
    
                list = dataContext.GetTable<TEntity>().Where(query).Skip(from).Take(to - from).ToList();            
    
                return list;
            }

    Required namespaces:

       using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Collections.Specialized;
        using System.ComponentModel;
        using System.Data.Linq;
        using System.Linq;
        using System.Linq.Expressions;
        using System.Reflection;

    If you think the project will help you; I will create something quickly with the above method and share on skydrive.

    Monday, December 23, 2013 9:48 AM
  • >>That being said; If you can please try it with this function:

    I think it may be difficult to run the Get() method, because we do not know how the DataContext class is defined.

    Is the DataLoadOptions a enum type?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 24, 2013 3:31 AM
    Moderator
  • I have created a new project and it's uploaded here: https://skydrive.live.com/redir?resid=40A5650560742617!97419&authkey=!APUwfIysSVnEzhA&ithint=file%2c.zip

    It is connecting to SQL DB which sits on Azure and it's created for this test. You may want to use your own Database as Azure won't allow connections if the IP is not on the list.

    If you can send me your IP, I can add you to the list if necessary.

    Tuesday, December 24, 2013 4:50 AM
  • Hello,

    Thanks for uploading the test file. I downloaded it and found that the reason why it throws message "type 'X' is not mapped as a Table", it is because that the DataContext is used in Linq To Sql framework rather than Linq To Entities, we cannot use them together.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 24, 2013 5:45 AM
    Moderator
  • Hi,

    Thank you for finding the solution so quickly. So, which one you recommend I change ? What is the best way to approach this solution ?

    Tuesday, December 24, 2013 6:11 AM
  • Hello,

    I recommend to use the Entity Framework and change the Get() method like below:

     public static IList<TEntity> Get<TEntity>(this DbContext db, Expression<Func<TEntity, bool>> query, string OrderBy, int from, int to) where TEntity : class,new()
            {
                DbSet<TEntity> entityDbSet = db.Set<TEntity>();
    
                IList<TEntity> list = null;
    
                if (to == 0)
                    to = 100;
    
                if (from > to)
                    from = 0;
    
                if (query == null)
                {
                    list = entityDbSet.OrderBy<TEntity>(OrderBy).Skip(from).Take(to - from).Cast<TEntity>().ToList();
                }
                else
                {
                    list = entityDbSet.Where(query).OrderBy<TEntity>(OrderBy).Skip(from).Take(to - from).Cast<TEntity>().ToList();
                }
                return list;
            }

    To add the OrderBy clause is because the method 'Skip' is only supported for sorted input in LINQ to Entities, it should be include an OrderBy before Skip.

    Since you want to query data dynamicly, we need to download the LINQ Dynamic Query Library from link below:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    We call it like below:

     using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
                {
                    var result = db.Get<Order>(order => order.OrderID == 1, "OrderID", 0, 1);
                }
    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by thukralz Tuesday, December 24, 2013 8:23 AM
    Tuesday, December 24, 2013 7:40 AM
    Moderator