Question

I'm trying to build a library project which will assist me in my other projects with some extension methods. The type which will be extended is DbContext. Here is some example

public bool Insert<TEntity>(this DbContext Context, TEntity entity) where TEntity : class
    {
        if (Context.Entry(entity).State == EntityState.Detached) //Entity is detached
        {
            Context.Set<TEntity>().Add(entity);
        }
        else //Entity is attached
        {
            Context.Entry(entity).State = EntityState.Added;
        }

        var str = Context.GetValidationErrors();

        if (Context.GetValidationErrors().Any()) return false;

        Context.SaveChanges();
        return true;

    }

Where is the problem ?

I'm exposing the context through WCF Data Services, and it shrinks my context capabilities, which for me is contraditory, since the goal here is to expose data, how can you expose data without the meanings to reach it ? So, how can i accomplish this task, exposing my entension methods as extensions methods to my client side context operations.

EDIT

I have been reading arround and found this answer from Ladislav Mrnka

Implement WCF Data Service using the Repository Pattern

Was it helpful?

Solution

DbContext API differs from API of generated context, when you're adding a reference to a data service. Moreover, possibilities of generated context are limited comparing to DbContext. Client side context is a helper for building OData queries, and it is not perfect. I don't think, that you could port every extension method without re-implementation (if it will be possible at all).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top