Reputation: 459
I look for a code to help me write a cs files(class) dynamically for each tables in database.
I heard we can do it by DQE(Dynamic Query Execution). but because I don't have any background about it, it's difficult for me to find the result.
Could you please give me a sample code about it.
Thank you
Upvotes: 0
Views: 170
Reputation: 389
You could do this using Entity Framework. I've done something similar in the past using Text Templates and the EF4.x POCO Generator. This was quite a while ago, and we were using EF4.x at the time (you should be using EF6 now), and used the ObjectContext generator (now it is recommended that you use the DbContext generator). In a nutshell, you set up the text templates to read from your chosen Database to dynamically build out classes that represent the entities defined in them.
Going over all of the steps to create ObjectContexts / DbContexts from text templates would bloat the heck out of this answer, and force me to rewrite the information already contained in the resources I've linked to. You can get the toolset with a few simple NuGet extension downloads Plus you'll learn better by doing a bit of research yourself :-D
In the end, you'll be able to write LINQ queries against your EF-generated objects similar to performing CRUD operations against a collection in C#. As a very simplistic example, if I have a Sales
database, and wanted to find records in my Orders
table that matched a specific field value I'm trying to filter by, I could do so with:
var myOrders = ( from ord in SalesContext.Orders
where ord.OrdField == myFieldVariableWithTheValueImLookingFor
select ord);
Hope that at least gets you started.
Upvotes: 2