The type 'X' is not mapped as a Table

烈酒焚心 提交于 2021-01-28 04:04:31

问题


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);

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?


回答1:


The real problem is that I was using DataContext in Linq To Sql framework rather than Linq To Entities, and it cannot be used together.

Explanation: http://social.msdn.microsoft.com/Forums/en-US/5588e2ce-00f7-42ba-9feb-4d33a80ddb05/the-type-x-is-not-mapped-as-a-table?forum=adodotnetentityframework




回答2:


It's because the type X doesn't have a primary key in your DB. Add a primary key to the table, update your model from database and try again.



来源:https://stackoverflow.com/questions/20677470/the-type-x-is-not-mapped-as-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!