2010-05-20 40 views
0

基本上我想使用一個動態數據網站來維護EF4模型中的實體在自己的程序集中的數據。模型和上下文在另一個程序集中。實體框架4獨立程序集中的POCO實體,動態數據網站?

我嘗試這樣Entity Framework 4 + Self-Tracking Entities + ASP.NET Dynamic Data = Error

但是從反射得到一個「不明確的匹配」錯誤:

System.Reflection.AmbiguousMatchException是由用戶代碼 消息未處理=歧義找到匹配。 源= mscorlib程序 堆棧跟蹤: 在System.RuntimeType.GetPropertyImpl(字符串名稱,的BindingFlags bindingAttr,粘結劑粘結劑類型返回類型,類型[]類型,ParameterModifier []改性劑) 在System.Type.GetProperty(字符串名稱) 在System.Web.DynamicData.ModelProviders.EFTableProvider..ctor(EFDataModelProvider dataModel,EntitySet entitySet,EntityType entityType,Type entityClrType,Type parentEntityClrType,Type rootEntityClrType,String name) at System.Web.DynamicData.ModelProviders.EFDataModelProvider.CreateTableProvider(EntitySet entitySet ,EntityType entityType) at System.Web.DynamicData.ModelProviders.EFDataModelProvider..ctor(Object contextInstance,Func 1 contextFactory) at System.Web.DynamicData.ModelProviders.SchemaCreator.CreateDataModel(Object contextInstance, Func 1 contextFactory) at System.Web.DynamicData .MetaModel.RegisterContext(Func`1 contextFactory,ContextConfiguration配置) 位於C:\ dev \ Puffin \ Puffin.Prototype.Web \ Global.asax.cs中的WebApplication1.Global.RegisterRoutes(RouteCollection routes):line 42 at WebApplication1。 C:\ dev \ Puffin \ Puffin.Prototype.Web \ Global.asax.cs中的Global.Application_Start(Object sender,EventArgs e):line 78 InnerException:

回答

1

我最近碰到類似的問題。它與我的模型中的繼承有關。我有一個Resource實體,派生了Person,Equipment等類型,在那些我重寫了一些屬性的情況下,卻錯誤地給了它們不同的簽名。我會描述我的情況,希望這會有所幫助。

爲了能夠足夠深調試到框架,並看到所有的變量值,你將不得不禁用優化:

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

我看到登記的背景下,當曖昧匹配錯誤Global.asax中爲你:

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的構造函數中的繼承層次結構。 (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(資源本身是一個層次)。

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

在對設備的部分類,我有:當這些屬性被加載

public virtual ICollection<Resource> Resources 
    { 

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

,但在父類,資源,資源被定義爲在IsPublicProperty的.GetProperty(propertyName的),它們具有相同的名稱但不同的簽名(因爲我給了他們錯誤地不同的返回類型),因此,目前尚不清楚這shoudl根據加載僅憑姓名。我糾正我的錯誤,並作出資源在我的設備類返回的ICollection和繁榮 - 沒有更多的曖昧匹配。

不知道這是否會幫助或沒有,但如果你在一個類似的方式逐步完成,你應該能夠準確地找到是什麼原因造成的模糊的匹配。