user1932923
user1932923

Reputation: 384

The type 'X' is not mapped as a Table

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?

Upvotes: 2

Views: 5387

Answers (2)

user1932923
user1932923

Reputation: 384

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

Upvotes: 3

James Dayeh
James Dayeh

Reputation: 526

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.

Upvotes: 0

Related Questions