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 8695583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:04:32+00:00 2026-06-13T01:04:32+00:00

Introduction I have a Client-Server architecture where the server stores its data in different

  • 0

Introduction

I have a Client-Server architecture where the server stores its data in different databases using Entity Framework 5.0. Therefore, it has different DbContext classes. However, there are some entity types shared amongst them. So we can have:

public class MyFirstDataAccess : DbContext
{
    //constructor omitted
    public DbSet<SharedType> MyEntities { get; set; }
    public DbSet<OtherType> MyOtherEntities { get; set; }
}
public class MySecondDataAccess : DbContext
{
    public DbSet<SharedType> MyEntitiesWithSharedType { get; set; }
}

Of course, the actual entities are not shared.

The problem

Different parts of the program need only parts of the data. E.g. one part could use the entities of Type SharedType. So I created a generic interface that provides the data:

public interface IEntityProvider<TEntity> where TEntity : class
{
    DbSet<TEntity> Elements { get; set; }

    void Dispose();
    int SaveChanges();
}

Implementing the interface in the DbContext I added the Elements property, which would replace e.g. the MyEntitiesWithSharedType property:

DbSet<SharedType> IEntityProvider<SharedType>.Elements { get; set; }

This actually works. Different parts of the program can now work with any DbContext as long as it provides the needed data.

I want to reuse some parts of the server in the client. Of course, the client does not have a DbContext, but retrieves its data via a WCF service from the server. Therefore, the DbSetcannot be used in the IEntityProvider interface, because this interface must be shared with the client. So I created another interface, which contains the DbSet's members I need:

public interface IEntityCache<TEntity> : IEnumerable<TEntity>, IQueryable<TEntity>
{
    ObservableCollection<TEntity> Local { get; }

    TEntity Add(TEntity entity);
    TEntity Remove(TEntity entity);
}

The problem is to get the DbSet as an IEntityCache. It cannot be cast directly, because the DbSet does not implement the interface, although it contains all its members. I tried some different approaches, but neither really worked:

Attempt 1

Create a subclass of DbSet, which implements the needed interface (–> class adapter).

This does not work, because DbSet does not have any public or protected constructors. Therefore, the adapter class is not valid.

Attempt 2

Instead of using an adapter class, I tried to use an adapter interface, which derives from IDbSet and the necessary interfaces:

public ICustomDbSet<SharedType> MyEntities{ get; set; }
IEntityCache<SharedType> IEntityProvider<SharedType>.Elements { get { return MyEntities; } }

This does not work, because EntityFramework does not set the ICustomDbSet. Probably because the value it would set, is no IEntityCache.

Solution

While writing this question, I found a pretty good solution (which I will post as an answer). So why do I post this question, though? Firstly, I would like to know, if there are any other approaches that seem more reasonable and if my solution has some drawbacks. Secondly, the solution might help someone else. And thirdly, writing this question took a while and it would be a pity to throw it away 🙂

  • 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-06-13T01:04:35+00:00Added an answer on June 13, 2026 at 1:04 am

    My solution is the consequent next step of the class adapter approaches: using an object adapter:

    public class DbSetAdapter<TEntity> : IEntityCache<TEntity> where TEntity : class
    {
        DbSet<TEntity> dbSet;
    
        public DbSetAdapter(DbSet<TEntity> dbSet)
        {
            this.dbSet = dbSet;
        }
    
        public ObservableCollection<TEntity> Local
        {
            get { return dbSet.Local; }
        }
    
        public TEntity Add(TEntity entity)
        {
            return dbSet.Add(entity);
        }
    
        public TEntity Remove(TEntity entity)
        {
            return dbSet.Remove(entity);
        }
    
        public IEnumerator<TEntity> GetEnumerator()
        {
            return ((IEnumerable<TEntity>)dbSet).GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)dbSet).GetEnumerator();
        }
    
        public Type ElementType
        {
            get { return ((IQueryable<TEntity>)dbSet).ElementType; }
        }
    
        public Expression Expression
        {
            get { return ((IQueryable<TEntity>)dbSet).Expression; }
        }
    
        public IQueryProvider Provider
        {
            get { return ((IQueryable<TEntity>)dbSet).Provider; }
        }
    }
    

    I used this adapter in my DbContext as follows:

    DbSetAdapter<SharedType> entityAdapter;
    ...
    public DbSet<SharedType> MyEntities{ get; set; }
    IEntityCache<SharedType> IEntityProvider<SharedType>.Elements { 
        get 
        {
            if (entityAdapter == null)
                entityAdapter = new DbSetAdapter<SharedType>(MyEntities);
            return entityAdapter; 
        }
    }
    

    This way, the IEntityCache can be implemented in a way that retrieves data from a service instead of a database. Furthermore, subparts of the program do not have to care about where their data come from.

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

Sidebar

Related Questions

Introduction : I have a Server [ WCF service library ] and Client [
Introduction We have an OpenID Provider which we created using the DotNetOpenAuth component. Everything
Introduction: I have an AdvancedDataGrid displaying hierarchical data illustrated by the image below: The
I'm having problems with sending data over SocketChannels between a Host and Client using
First a little introduction. We have an SQL Server Express 2008 database, which schema
Only the Flash Media Enterprise Server(FMES) has the RTMFP peer introduction services http://www.adobe.com/products/flashmediaserver/compare/ influxis
Introduction I have a app that has multiple tables, some with and some without
Introduction : I have prior experience in programming (C, C++, Java), however, this is
Introduction I have been so annoyed by applications that have a startup dialog which
Introduction I have some sort of values that I might want to access several

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.