我想使用单元可测试的 EF 4.1 代码优先模式。我遵循了本文答案中说明的代码示例。但是现在我收到一个错误,指出"不支持每种类型的多个对象集"。我也在使用最新的 IoC 结构图。我不确定如何解决此问题。
每个类型有多个对象集不是 支持。对象集 "成员类型"和"成员类型"可以 两者都包含类型的实例 "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 2010SocialSiteSocialSite.DataRepositoriesImplRepository.cs:84
SocialSite.Service.Impl.MemberTypesService.GetMemberTypes() in D:Visual Studio 2010SocialSiteSocialSite.ComponentsImplMemberTypesService.cs:23
SocialSite.Web.Areas.Admin.Controllers.MemberTypesController.ListMemberTypes() in D:Visual Studio 2010SocialSiteSocialSiteAreasAdminControllersMemberTypesController.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; }
}
异常表示您的DbContext
(或者可能是从上下文中引用的某个类)定义了两次DbSet<MemberType> MemberType
。每种类型只能有一个已定义的DbSet
。您发布的代码与异常完全无关。当 EF 代码首次浏览DbContext
并从可用DbSets
创建模型元数据时,将引发异常