0

基本上我想使用动态数据网站来维护 EF4 模型中的数据,其中实体位于它们自己的程序集中。模型和上下文在另一个程序集中。

我试过这个Entity Framework 4 + Self-Tracking Entities + ASP.NET Dynamic Data = Error

但从反射中得到一个“模糊匹配”错误:

System.Reflection.AmbiguousMatchException 未被用户代码 Message=Ambiguous match found 处理。Source=mscorlib StackTrace:在 System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) at System.Type.GetProperty(String name) at System.Web.DynamicData .ModelProviders.EFTableProvider..ctor(EFDataModelProvider dataModel, EntitySet entitySet, EntityType entityType, Type entityClrType, Type parentEntityClrType, Type rootEntityClrType, String name) 在 System.Web.DynamicData.ModelProviders.EFDataModelProvider.CreateTableProvider(EntitySet entitySet, EntityType entityType) 在 System .Web.DynamicData.ModelProviders.EFDataModelProvider..ctor(对象上下文实例,Func1 contextFactory) at System.Web.DynamicData.ModelProviders.SchemaCreator.CreateDataModel(Object contextInstance, Func1 contextFactory) 在 System.Web.DynamicData.MetaModel.RegisterContext(Func`1 contextFactory, ContextConfiguration configuration) 在 C:\dev\Puffin\Puffin.Prototype.Web\Global.asax 中的 WebApplication1.Global.RegisterRoutes(RouteCollection routes)。 c:\dev\Puffin\Puffin.Prototype.Web\Global.asax.cs 中 WebApplication1.Global.Application_Start(Object sender, EventArgs e) 的第 42 行:第 78 行 InnerException:

4

1 回答 1

1

我最近遇到了与此类似的问题。它与我模型中的继承有关。我有一个 Resource 实体,它派生了 Person、Equipment 等类型,在这些实体中我覆盖了几个属性,但错误地给了它们不同的签名。我将描述我的场景,希望它会有所帮助。

为了能够对框架进行足够深入的调试并查看所有变量值,您必须禁用优化:

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

在 Global.asax 中注册 Context 时,我看到了 Ambiguous Match 错误:

    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 (i.e. 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 });

进入 RegisterContext 方法,我进入 System.Web.DynamicData.ModelProviders.EFDataModelProvider 有一段代码通过遍历 EFDataModelProvider 的构造函数中的继承层次结构来加载模型中的所有实体。

while (objectStack.Any()) { EntityType entityType = objectStack.Pop(); if (entityType != null) { // 当我们处于另一个根类型(没有基类型的类型)时更新实体集。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); 
            }
        } 

我在这里放了一个断点,并且能够看到在我的设备实体(从资源派生)上调用 CreateTableProvider 时发生了不明确的匹配。

从原始异常回顾堆栈跟踪(我应该首先完成!)我在 System.Web.DynamicData.ModelProviders.EFTableProvider.IsPublicProperty 的构造函数中放置了一个断点并观察了哪个属性/方法/不管是什么导致了模棱两可的匹配——对我来说,这最终成为了一个名为 Resources 的导航属性(资源本身就是一个层次结构),我在 Equipment.xml 中重写了它。

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

在设备的部分课程中,我有:

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

但是在父类Resource中,Resources被定义为:

    public virtual ICollection<Resource> Resources
    {

当这些属性由 IsPublicProperty 中的 .GetProperty(propertyName) 加载时,它们具有相同的名称但不同的签名(因为我错误地给了它们不同的返回类型)所以不清楚仅根据名称加载哪个. 我纠正了我的错误并让我的 Equipment 类中的 Resources 返回一个 ICollection,然后繁荣——不再是模棱两可的匹配。

不确定这是否有帮助,但如果您以类似的方式逐步完成,您应该能够准确找到导致模棱两可匹配的原因。

于 2010-05-27T23:17:33.157 回答