首先是ASP.Net MVC 3 EF4.1代码 - 不支持每种类型的多个对象集

时间:2011-05-09 15:31:01

标签: asp.net-mvc-3 entity-framework-4.1

我想使用可以单元测试的EF 4.1代码第一个模式。我按照This Post的答案中所示的代码示例进行了操作。但现在我收到一条错误,指出“不支持每种类型的多个对象集”。我也在使用最新的IoC结构图。我不知道如何解决这个问题。

  

每种类型的多个对象集不是   支持的。对象集   'MemberTypes'和'MemberTypes'可以   两者都包含类型的实例   'SocialSite.Model.Entities.MemberType'。

Line 82: public virtual IEnumerable<T> FindAll()
Line 83: {
Line 84:     return _dbSet.ToList();
Line 85: }
Line 86: 

[InvalidOperationException: Multiple object sets per type are not supported. The     object sets 'MemberTypes' and 'MemberTypes' can both contain instances of type     'SocialSite.Model.Entities.MemberType'.]
   System.Data.Entity.Internal.DbSetDiscoveryService.RegisterSets(DbModelBuilder     modelBuilder) +336
   System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder() +393
   System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext     internalContext) +22
   System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +117
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +407
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +17
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +62
   System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +15
   System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +315
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
   SocialSite.Data.Repositories.Impl.Repository`1.FindAll() in D:\Visual Studio 2010\SocialSite\SocialSite.Data\Repositories\Impl\Repository.cs:84
   SocialSite.Service.Impl.MemberTypesService.GetMemberTypes() in D:\Visual Studio 2010\SocialSite\SocialSite.Components\Impl\MemberTypesService.cs:23
   SocialSite.Web.Areas.Admin.Controllers.MemberTypesController.ListMemberTypes() in D:\Visual Studio 2010\SocialSite\SocialSite\Areas\Admin\Controllers\MemberTypesController.cs:44
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

不确定如何格式化堆栈跟踪,而不是那个。

这是有问题的模型。

public class MemberType
{
    [Key]
    public virtual int Id { get; set; }

    [StringLengthAttribute(65, ErrorMessage = "Maximum length 65 characters")]
    public virtual string Name { get; set; }

    public virtual string Description { get; set; }

    public virtual bool Registration { get; set; }

    public virtual bool Enabled { get; set; }

    [TimestampAttribute]
    public virtual byte[] Timestamp { get; set; }

    public virtual bool Activation { get; set; }

    public virtual Guid RoleId { get; set; } // TODO: this should be a complex type of aspnet_Role or atleaset a foriegn key

    public virtual bool Searchable { get; set; }

    public virtual ICollection<MemberTypeProfileFieldGroup> MemberTypeProfileFieldGroups { get; set; }
}

1 个答案:

答案 0 :(得分:12)

例外情况表明你的DbContext(或者你的上下文引用的某些类定义了DbSet<MemberType> MemberType两次。每种类型只能有一个定义的DbSet。你发布的代码绝对没有任何关系当EF代码首次探索DbContext并从可用的DbSets

创建模型元数据时抛出异常。