Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:46:37+00:00 2026-05-16T15:46:37+00:00

Is it possible to create a dynamic query with Entity Framework. I Have 18

  • 0

Is it possible to create a dynamic query with Entity Framework. I Have 18 tables, Each of them has the same structures. How can I create a dynamic query to reuse the same query for each tables. I would like to have a generic query for Create Read Update Delete.
The read contains the same “Where” Clause.
Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:46:37+00:00Added an answer on May 16, 2026 at 3:46 pm

    Here you have simple example for pure CRUD scenario. Create interface which will contain shared properties for your queries. Implement this interface in all your entity classes. Than create repository. Repository is usually defined as generic but in your case I defined each method as generic so that you can use same repository instence for all entities.

    public interface IWellKnownEntity
    {
      int Type { get; set; }
    }
    
    public class Repository
    {
      public T GetEntityByWellKnownQuery<T>() where T : IWellKnownEntity
      {
        using (var context = new MyContext())
        {
          return context.CreateObjectSet<T>().FirstOrDefault(e => e.Type == 1);
        }
      }
    
      public IEnumerable<T> GetEntitiesByCustomQuery<T>(Expression<Func<T, bool>> where)
      {
        using (var context = new MyContext())
        {
          return context.CreateObjectSet<T>().Where(where).ToList();
        }
      }
    
      public void Create<T>(T entity) where T : IWellKnownEntity
      {
        using (var context = new MyContext())
        {
          context.AddObject(entity);
          context.SaveChanges();
        }
      }
    
      public void Update<T>(T entity) where T : IWellKnownEntity
      {
        using (var context = new MyContext())
        {
          context.Attach(entity);
          context.ObjectStateManager.ChageObjecState(entity, EntityState.Modified);
          context.SaveChanges();
        }
      }
    
      public void Delete<T>(T entity) where T : IWellKnownEntity
      {
        using (var context = new MyContext())
        {
          context.Attach(entity);
          context.DeleteObject(entity);
          context.SaveChanges();
        }
      }
    }
    

    Than you suppose that you have entity product and catebory which impelment well known interface. You can simply call:

    var repository = new Repository();
    
    var product = repository.GetEntityByWellKnownQuery<Product>();
    product.Name = "Updated";
    repository.Update<Product>(product);
    
    var category = repository.GetEntitiesByCustomQuery<Category>(c => c.Id == 1).First();
    repository.Delete<Category>(category);
    

    You can futher improve the sample code. This code doesn’t use shared context so it is more usable for disconnected scenario (web application). If you use connected scenario like WinForms application or batch application you can implement IDisposable on repository and share context among all methods. Dispose method on repository will hanlde disposing of context. Code for Update and Delete methods will be different because there is no need to attach entity back to context or set entity state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if it's possible to create a dynamic linq query using linq to objects.
Possible Duplicate: How can I make this query to accept dynamic table names? I
is it possible in Android create dynamic layouts and then use them? I mean
In ASIO, s it possible to create another socket that has the same source
i am asking myself if it is possible to somehow create a dynamic gui
Possible Duplicate: Create an alert on any view controller after Facebook request:didFailWithError: I have
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to
I have create a LINQ-to-SQL project in Visual Studio 2010 using Dynamic Data. In
I have a form that has all the fields pulled dynamic from a database
Is it possible to create dynamic js file creation? e.g. I am referring MyJs.js

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.