0

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

5
  • What's the RDBMS? Sql Server? Commented Feb 21, 2014 at 14:36
  • 1
    Have you ever heard of entity framework? You could use T4s to generate a managed-code representation of your database and tables. Commented Feb 21, 2014 at 14:37
  • it's about managing in SQL Commented Feb 21, 2014 at 14:38
  • @EricStallcup could u pls give me a link as a reference Commented Feb 21, 2014 at 14:39
  • @EricStallcup I found this article. if u know better than this one, let me know. thx Commented Feb 21, 2014 at 14:59

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it's interesting and thank u for ur useful answer. Now attached diagram, classes, SP_Result.cs. I have a question, How should I continue to save my data on database and use stored procedures?! should I do it same as before and make a access layer or there is other better way?
This article was very useful for me. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.