void
void

Reputation: 43

How to use Entity Framework to query a table having dynamic columns?

I have a table named Tags in which a new column is added when a user creates a tag (name of the column will be the tag name that the user gave). Also, the column is renamed when a user edits a tag name.

I know there is a design flaw in my application. But just wondering how are we supposed to create a model for such a table? Is there any way I could perform CRUD operation in such table using Entity Framework?

Upvotes: 0

Views: 486

Answers (1)

Piotr Stapp
Piotr Stapp

Reputation: 19828

Except it is a very strange design the solution is quite simple - just execute raw SQL query, and do the manual mapping. Example query:

    string studentName = ctx.Database
           .SqlQuery<string>("Select tag1 from Posts where postId=@id", new SqlParameter("@id", 1))
           .FirstOrDefault();

All CRUD operations you can handle with raw queries.

Upvotes: 1

Related Questions