实体框架4个POCO实体分开装配,动态数据网站? [英] Entity Framework 4 POCO entities in separate assembly, Dynamic Data Website?

查看:254
本文介绍了实体框架4个POCO实体分开装配,动态数据网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试了这个



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



System.Reflection.AmbiguousMatchException被用户代码未处理
消息=发现不明确的匹配项。
Source = mscorlib
StackTrace:
在System.RuntimeType.GetPropertyImpl(String name,BindingFlags bindingAttr,Binder binder,Type returnType,Type [] types,ParameterModifier []修饰符)
(SystemNameType.GetProperty b $ b在System.Web.DynamicData.ModelProviders.EFDataModelProvider.CreateTableProvider(EntitySet entitySet,EntityType entityType)
在System.Web.DynamicData.ModelProviders.EFDataModelProvider..ctor(Object contextInstance,Func 1 contextFactory)
在System.Web.DynamicData.ModelProviders.SchemaCreator.CreateDataModel(Object contextInstance,Func
1 contextFactory)
在System.Web.DynamicData.MetaModel.RegisterContext(Func `1 contextFactory,ContextConfigura C:\dev\Puffin\Puffin.Prototype.Web\Global.asax.cs中的WebApplication1.Global.RegisterRoutes(RouteCollection路由)中的
:WebApplication1上的第42行
。 Global.Application_Start(Object sender,EventArgs e)in C:\dev\Puffin\Puffin.Prototype.Web\Global.asax.cs:line 78
InnerException:

解决方案

最近我遇到了类似的问题。它与我的模型中的继承有关。我有一个资源实体,派生类型的人,设备等,在那些我已经覆盖了几个属性,但错误地给了他们不同的签名。我将描述我的场景,并希望它能够帮助。



为了能够在框架中进行足够的调试,并看到所有的变量值,你将不得不禁用优化:



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



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



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 ASP.NET动态数据的.NET实体框架模型。
//设置ScaffoldAllTables =只有当您确定您希望
//数据模型中的所有表支持支架(即模板)视图时,才为true。要控制
//个别表的脚手架,请为表创建一个部分类,并将
// [ScaffoldTable(true)]属性应用于部分类。
//注意:确保将YourDataContextType更改为应用程序中数据上下文
//类的名称。
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(派生类Python中的EntityType derivedEntityType [entityType]){
//将栈上的派生实体类型推送
objectStack.Push(derivedEntityType);
}
}

我在这里放了一个断点,看到在我的设备实体(从资源派生)中调用CreateTableProvider时,发生了模糊匹配。



回顾原始异常中的堆栈跟踪应该首先做的!)我在System.Web.DynamicData.ModelProviders.EFTableProvider.IsPublicProperty的构造函数中放置了一个断点,并观察哪个属性/方法/任何导致模糊匹配的东西 - 对我而言作为一个导航属性,称为资源(资源本身是一个层次结构),我已经在设备中被覆盖了。

  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>资源
{

但在父类中,Resource,Resources定义为: p>

  public virtual ICollection< Resource>资源
{

当IsPublicProperty中的.GetProperty(propertyName)加载这些属性时,它们具有相同的名称但不同的签名(因为我错误地给了它们不同的返回类型),所以不清楚哪个shoudl是基于名称加载的。我更正了我的错误,并使我的设备类中的资源返回ICollection,并且繁荣 - 没有更加模糊的匹配。



不知道这是否会有所帮助,但如果你可以通过类似的方式逐步完成你应该能够准确找到导致模糊匹配的原因。


Basically I want to use a dynamic data website to maintain data in an EF4 model where the entities are in their own assembly. Model and context are in another assembly.

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

but get an "ambiguous match" error from reflection:

System.Reflection.AmbiguousMatchException was unhandled by user code Message=Ambiguous match found. Source=mscorlib StackTrace: at 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) at System.Web.DynamicData.ModelProviders.EFDataModelProvider.CreateTableProvider(EntitySet entitySet, EntityType entityType) at System.Web.DynamicData.ModelProviders.EFDataModelProvider..ctor(Object contextInstance, Func1 contextFactory) at System.Web.DynamicData.ModelProviders.SchemaCreator.CreateDataModel(Object contextInstance, Func1 contextFactory) at System.Web.DynamicData.MetaModel.RegisterContext(Func`1 contextFactory, ContextConfiguration configuration) at WebApplication1.Global.RegisterRoutes(RouteCollection routes) in C:\dev\Puffin\Puffin.Prototype.Web\Global.asax.cs:line 42 at WebApplication1.Global.Application_Start(Object sender, EventArgs e) in C:\dev\Puffin\Puffin.Prototype.Web\Global.asax.cs:line 78 InnerException:

解决方案

I came across a similar problem to this recently. It had to do with inheritance in my model. I had a Resource entity that had derived types of Person, Equipment, etc. and in those I had overridden a couple properties, but by mistake gave them different signatures. I'll describe my scenario and hopefully it will help.

To be able to debug deep enough into the framework, and see all the variable values, you will have to disable optimizations:

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

I was seeing the Ambiguous Match error when registering the Context in Global.asax as you were:

    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 });

Stepping into the RegisterContext method, I got to System.Web.DynamicData.ModelProviders.EFDataModelProvider there is section of code that loads all the Entities in the model by traversing the inheritance hierarchy in the constuctor for EFDataModelProvider.

while (objectStack.Any()) { EntityType entityType = objectStack.Pop(); if (entityType != null) { // Update the entity set when we are at another root type (a 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 put a breakpoint in here and was able to see that Ambiguous Match was occurring for me when calling CreateTableProvider on my Equipment entity (which was derived from Resource).

Looking back at the Stack Trace from the original exception (which I should have done in the first place!) I put a breakpoint in the constructor for System.Web.DynamicData.ModelProviders.EFTableProvider.IsPublicProperty and watched to see which property/method/whatever was causing the ambiguous match -- for me this ended up being a navigation property called Resources (Resources are themselves a hierarchy) that I had overridden in Equipment.

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

In the 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 being loaded by the .GetProperty(propertyName) in IsPublicProperty, they have the same name but different signatures (because I had given them different return type by mistake) so it isn't clear which shoudl be loaded based on name alone. I corrected my mistake and made Resources in my Equipment class return an ICollection, and boom -- no more ambiguous match.

Not sure if this will help or not, but if you step through in a similar way you should be able to find exactly what is causing the ambiguous match.

这篇关于实体框架4个POCO实体分开装配,动态数据网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆