Entity Framework 4 POCO object in a separate assembly, dynamic data site?

Basically, I want to use a dynamic data site to store data in the EF4 model, where the objects are in their own assembly. The model and context are in a different assembly.

I tried this Entity Framework 4 + Self-Tracking Entities + Dynamic Data ASP.NET = Error

but we get an “ambiguous coincidence” error from reflection:

System.Reflection.AmbiguousMatchException failed to execute user code Message = ambiguous match found. Source = mscorlib Stack Traces: in System.RuntimeType.GetPropertyImpl (String name, BindingFlags bindingAttr, Binder Binding, Return Type Type, Type [], ParameterModifier [] Modifiers) in System.Type.GetProperty (string name) in System.Web .DynamicData.ModelProviders.EFTableProvider..ctor (EFDataModelProvider dataModel, EntitySet entitySet, EntityType entityType, Type entityClrType, Type parentEntityClrType, rootEntityClrType type string Name) in System.Web.DynamicData.ModelProviders.EFDataModelProvider.CreateTableProvider (EntitySet entitySet, EntityType entityType) in System.Web.DynamicData.ModelProviders.EFDataModelProvider..ctor (Object contextInstance, Func 1 contextFactory) at System.Web.DynamicData.ModelProviders.SchemaCreator.CreateDataModel(Object contextInstance, Func 1 contextFactory) in System.Web.Daic. RegisterContext (Func`1 contextFactory, ContextConfiguration configuration) in WebApplication1.Global.RegisterRoutes (RouteCollection routes) in C: \ dev \ Puffin \ Puffin.Prototype.Web \ Global.asax.cs: line 42 in WebApplication1.Global.Application_Start (sender object, EventArgs e) in C: \ dev \ Puffin \ Puffin.Prototype.Web \ Global.asax.cs: line 78 InnerException:

0
source share
1 answer

Recently, I have encountered a similar problem. This is due to inheritance in my model. I had a Resource object that received the types Person, Equipment, etc., And in those cases when I redefined a couple of properties, but mistakenly gave them different signatures. I will describe my scenario and hope this helps.

To be able to debug the framework deep enough and see all the values ​​of variables, you will have to disable optimization:

http://blogs.msdn.com/b/kirillosenkov/archive/2009/01/27/how-to-disable-optimizations-during-debugging.aspx

I saw an ambiguous match error when registering a Context in Global.asax like you:

  public static void RegisterRoutes(RouteCollection routes) { // IMPORTANT: DATA MODEL REGISTRATION // Uncomment this line to register an ADO.NET Entity Framework model for ASP.NET Dynamic Data. // Set ScaffoldAllTables = true only if you are sure that you want all tables in the // data model to support a scaffold (ie templates) view. To control scaffolding for // individual tables, create a partial class for the table and apply the // [ScaffoldTable(true)] attribute to the partial class. // Note: Make sure that you change "YourDataContextType" to the name of the data context // class in your application. DefaultModel.RegisterContext(typeof(EntityModelContainer), new ContextConfiguration() { ScaffoldAllTables = true }); 

Entering the RegisterContext method, I got into System.Web.DynamicData.ModelProviders.EFDataModelProvider, there is a section of code that loads all the objects in the model, moving the inheritance hierarchy to constuctor for EFDataModelProvider.

while (objectStack.Any ()) {EntityType entityType = objectStack.Pop (); if (entityType! = null) {// Update the set of objects when we are in a different root type (type without a base type). if (entityType.BaseType == null) {currentEntitySet = entitySetLookup [entityType]; }

  var table = CreateTableProvider(currentEntitySet, entityType); tables.Add(table); } foreach (EntityType derivedEntityType in derivedTypesLookup[entityType]) { // Push the derived entity types on the stack objectStack.Push(derivedEntityType); } } 

I set a breakpoint here and was able to see that there was an ambiguous match for me when I called CreateTableProvider on my Equipment object (which was obtained from Resource).

Looking back at Stack Trace from the original exception (which I should have done first!), I set a breakpoint in the constructor for System.Web.DynamicData.ModelProviders.EFTableProvider.IsPublicProperty and looked at which property / method / caused something ambiguous coincidence - for me it turned out to be the navigation property Resources (Resources in themselves are a hierarchy), which I redefined in Equipment.

  private static bool IsPublicProperty(Type entityClrType, string propertyName) { var property = entityClrType.GetProperty(propertyName); return property != null && property.GetGetMethod() != null; } 

In a partial class for equipment, I had:

 public partial class Equipment { public new IEnumerable<Resource> Resources { 

but in the parent class Resource, Resources was defined as:

  public virtual ICollection<Resource> Resources { 

When these properties are loaded using .GetProperty (propertyName) in IsPublicProperty, they have the same name but different signatures (because I gave them a different return type by mistake), so it is not clear which shoudl will be loaded based on only by the name. I fixed my mistake and made Resources in my equipment class returning ICollection, and the arrow is no more than an ambiguous match.

Not sure if this will help or not, but if you go the same way, you can determine exactly what is causing an ambiguous match.

+1
source

All Articles