error loading database initializer with EF6
我一直在尝试遵循本教程 … http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data- model-for-an-asp-net-mvc-application 但我不断收到以下错误…
system.invalidoperationexception = {“Failed to set database
initializer of type ‘WeddingPreparations.Dal.WeddingInitializer,
KevinLisaWedding’ for DbContext type
‘WeddingPreparations.Dal.WeddingContext’ specified in the application
configuration. See inner exception for details.”}
内部异常是…
{“Could not load type ‘WeddingPreparations.Dal.WeddingContext’ from
assembly ‘EntityFramework, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089′.”:”WeddingPreparations.Dal.WeddingContext”}
堆栈跟踪是…
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage)
at System.Data.Entity.Internal.InitializerConfig.<>c__DisplayClass6.<TryGetInitializerFromEntityFrameworkSection>b__1(ContextElement e) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Internal.InitializerConfig.TryGetInitializerFromEntityFrameworkSection(Type contextType) at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type contextType) at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name) at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService(IDbDependencyResolver resolver, Type type) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at KevinLisaWedding.Controllers.HomeController.Index() in c://Users//kdonde//Documents//Visual Studio 2013//Projects//KevinLisaWedding//KevinLisaWedding//Controllers//HomeController.cs:line 14 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e() |
我也搜索了高低,为什么我没有任何运气就收到这个错误,我已经下载了完成的项目并将它与我的项目进行了比较,但没有任何运气。
我目前的代码如下。
在WeddingPreparations项目中…
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace WeddingPreparations.Dal { public class WeddingContext : DbContext { public WeddingContext() : base("WeddingContext") { } // Any entities reference by this class will be implicitly included. protected override void OnModelCreating(DbModelBuilder modelBuilder) |
和
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
namespace WeddingPreparations.Dal { public class WeddingInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<WeddingContext> { protected override void Seed(WeddingContext context) { //base.Seed(context); var invitees = new List<Invitee> { new Invitee{ FirstName="FName1", LastName="LName1", Attending = true, HotelRoomRequired= true, Notes="I’m the groom! :)"}, new Invitee{ FirstName="FName2", LastName="LName2", Attending = true, HotelRoomRequired= true, Notes="I’m the bride! :)"} }; |
我在这个项目中的模型如下
1
2 3 4 5 6 7 8 9 10 11 12 |
namespace WeddingPreparations.Models
{ public class Invitee { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public bool Attending { get; set; } public bool HotelRoomRequired { get; set; } public string Notes { get; set; } } } |
在 mvc 项目中,我有以下 web.config 文件…
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<?xml version="1.0" encoding="utf-8"?> <!– For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=301880 –> <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> |
在我的家庭控制器中,我正在执行以下代码…
1
2 |
WeddingPreparations.Dal.WeddingContext ctx = new WeddingPreparations.Dal.WeddingContext();
var x = ctx.Invitees.ToList(); |
如果我没有弄错,它应该创建数据库并使用初始化程序中指定的数据为其播种……但是我得到的只是那个错误,我似乎无法弄清楚…… . 任何方向都值得赞赏。
更新
我尝试像在教程中那样重新创建项目,但我仍然遇到同样的错误。肯定有一些代码丢失了……要么我错过了它……要么教程没有它。
在你的
1
2 3 4 5 |
<contexts>
<context type="WeddingPreparations.Dal.WeddingContext"> <databaseInitializer type="WeddingPreparations.Dal.WeddingInitializer, KevinLisaWedding" /> </context> </contexts> |
这表示在名为
1
2 3 4 5 |
<contexts>
<context type="WeddingPreparations.Dal.WeddingContext, WeddingPreparations"> <databaseInitializer type="WeddingPreparations.Dal.WeddingInitializer, WeddingPreparations" /> </context> </contexts> |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/269613.html