3

I have a standard model set.

I have a base context class that inherits from dbcontext to add some features I needed.

public class MyContext : DbContext
{
    public void MyFeature() {
    }
}

I then have my actual Data Context:

public class DataContext : MyContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

I want to use the scaffolder built in when you create a controller, but I get an error "Unsupported Context Type" If I change the datacontext to just inherit from dbcontext directly it works, but at this point I have alot of stuff that uses the added features, so changing the inheritance cant be done without commenting out all that stuff. And I have of course simplified down the features, it is actually quite alot of stuff, so adding it directly into the datacontext would be alot of work, plus the scaffolder should be smart enough to see that the datacontext is a dbcontext.

How can I use the scaffolder with my datacontext?

undefined
  • 33,537
  • 22
  • 129
  • 198
Solmead
  • 4,158
  • 2
  • 26
  • 30

2 Answers2

2

Why don't you use Composition?

If your feature really is just needed as lets say a few methods needed in those objects I would put those methods in a separate class called ContextDetails - something along those lines and have DataContext contain a ContextDetails like so:

//Changed MyContext to ContextDetails
public class ContextDetails 
{
    public void MyFeature() 
    {
        //Do something
    }
}

public class DataContext : DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }

    public ContextDetails DbDetails { get; set; }
}

And if the ContextDetails object needs information about the DataContext/DbContext it's in pass the DataContext/DbContext into a method or even the constructor.

If you don't like Composition for this problem maybe you want to use an Interface. If that's the case check out http://www.codeproject.com/Tips/309753/Repository-Pattern-with-Entity-Framework-4-1-and-C

The context class must inherit from System.Data.EntityDbContext which provides facilities for querying and working with entity data as objects

The best reason I could find for why the inheritance is not working in your example.

EDIT:

I read my answer and realized DBDetails might not be the best name but you get the idea. Extract the implementation and use it as a separate entity. Good luck!

Nate-Wilkins
  • 5,364
  • 4
  • 46
  • 61
  • I'm thinking your right, composition with an interface is the only way. Use an interface on the DataContext that has the DbDetails as a property, and then have the DbDetails have the extra things needed. – Solmead Sep 27 '12 at 16:42
  • @Solmead I'm sure its not the only way but I definitely would like this way. Good luck! – Nate-Wilkins Sep 27 '12 at 18:16
0

i think first you should install entity framework 4.0 then i think definitely it's working please try this.

Rajpurohit
  • 1,951
  • 2
  • 16
  • 19