0

My project calls for a Core Application that acts as a central point for small app style applications to interact with a much larger central system. These are TV applications so don't think mobile. Most applications will be able to reference this core without needing any additional code. However, some of the applications (which are not known at this time) will most likely need to have additional functionality. I want to support this additional functionality with a Plug-in model that allows for extending the functionality of the Core without changing the Core. Since the source may not be available to the Plug-in developer. I want to use EF Code First to allow the plug-ins to extend the data model.

For example I might define a User entity and they may need to extend it to support CustomUser:User. I would like to build the table for this like:

modelBuilder.Entity<User>()
.Map(m => m.ToTable("User"))
.Map<CustomUser>(m => m.ToTable("CustomUser")); // Would like this portion provided by plug-in

I know this works if you do it all at once, but I don't see how to pull in my mapping from the plug-in . I have tried seperating it into two statements like:

Core

modelBuilder.Entity<User>()
.Map(m => m.ToTable("User"));

Plugin

modelBuilder.Entity<User>()
.Map<CustomUser>(m => m.ToTable("CustomUser"));

However, when this runs I get an error stating that the User entity must be defined in a single mapping statement.

So up until now I have been trying to figure out how to pass just the mapping info in from the Plug-in before I define my User entity, but don't see a way to pass it in at runtime since the Core project doesn't know about the Plug-ins "CustomUser".

If further clarification is needed please ask.

Edit 1: As a point of information. I followed this post here and can replicate its functionality, but it doesn't allow me to build the relationships that I'm after. Unless I'm missing something.

Edit 2: Doing additional research I found that I can build my mapping configuration as in this post, but I can't figure out how to pass my configuration in once created. I'm currently using MEF, but I'm open to trying a different method. I have heard that Ninject can do this sort of thing, but I'm not clear on how that works.

Community
  • 1
  • 1
jalewis
  • 103
  • 1
  • 1
  • 8
  • Have you considered using stored procedures or setting up an sql statement with inputs to handle this? I think you are fighting the framework a little with trying to get dynamic use out of lambda expressions. – Travis J Mar 06 '12 at 22:55
  • @TravisJ I'm not locked into using the lambda's I am open to other methods. This was just the easiest way for me to demonstrate my end goal. I'm not sure how to do what you suggest. Could you elaborate? – jalewis Mar 06 '12 at 23:14
  • It would pretty much be similar to the approach you take. The adapter for the plugin would take in the existing entity, a new entity and a new field. Then you would use a c# sql statement to build the table creation with relevant fields. I must admit, straight sql statements and stored procedures are not a strength of mine. They are, however, extremely powerful and able to accomplish the type of dynamic table creation you are looking for. – Travis J Mar 06 '12 at 23:19
  • @TravisJ Ok I see what you mean. Is it possible to mix EF with this model? Or are we talking about completely abandoning EF? Hmm... Or would this basicly be using EF DB first with a call to update the model after plugins generate the new dynamicly built tables? I appreciate the help with thinking down new paths. – jalewis Mar 06 '12 at 23:29

1 Answers1

0

Pardon me if I've completely misunderstood you, but I think you're looking for the following. We have something very like this working in our solution.

Core

modelBuilder.Entity<User>()
    .Map(m => m.ToTable("User"));

Plugin

modelBuilder.Entity<CustomUser>()
    .Map(m => m.ToTable("CustomUser"));

where

public class CustomUser : User {}