繁体   English   中英

C# MVC 与实体框架数据库初始化程序失败

[英]C# MVC with Entity Framework Database Initializer failing

我在一个网站上作为一个学习工具工作,并且遇到了实体框架无法设置数据库初始化程序的问题。 这是我得到的错误:

 System.InvalidOperationException
 HResult=0x80131509
 Message=Failed to set database initializer of type 'WhatsInStorage.DAL.DatabaseInitializer, WhatsInStorage' for DbContext type 'WhatsInStorage.DAL.DatabaseContext, WhatsInStorage' specified in the application configuration. See inner exception for details.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>

Inner Exception 1:
TypeLoadException: Could not load type 
'WhatsInStorage.DAL.DatabaseInitializer' from assembly 'WhatsInStorage'.

为了创建我的 controller,我使用实体框架添加了一个新的 controller 和 MVC5 Controller 和视图。 作为 model class,我使用了 Box(它显示在下拉菜单中),对于数据上下文 class,我使用了 DatabaseContext(预填充)。

这是 model Box.cs:

 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;

 namespace WhatsInStorage.Models
 {
     public class Box
     {
         [Key]
         public int BoxId { get; set; }
         public int RoomNumber { get; set; }
         public List<Item> Items { get; set; }
     }
 }

这是 model Item.cs:

 using System.ComponentModel.DataAnnotations;

 namespace WhatsInStorage.Models
 {
     public class Item
     {
         [Key]
         public int BoxID {get; set;}
         public string ItemName { get; set; }
         public int Quantity { get; set; }

     }
 }

这是我的 DatabaseContext.cs 中的代码:

 using System.Data.Entity;
 using System.Data.Entity.ModelConfiguration.Conventions;
 using WhatsInStorage.Models;

 namespace WhatsInStorage.DAL
 {
     public class DatabaseContext : DbContext
     {
         public DatabaseContext() : base("DatabaseContext") 
         {
         }

         public DbSet<Rooms> Rooms { get; set; }
         public DbSet<Item> Item { get; set; }
         public DbSet<Box> Box { get; set; }
     }
 }

这是 DatabaseInitalizer.cs 中的代码:

 using System.Collections.Generic;
 using WhatsInStorage.Models;

 namespace WhatsInStorage.DAL
 {
     public class DatabaseInitalizer : System.Data.Entity.DropCreateDatabaseIfModelChanges<DatabaseContext>
     {
         protected override void Seed(DatabaseContext context)
         {
             var InitRoom = new List<Rooms>()
             {
                 new Rooms{RoomID=1, RoomName="Bedroom 1"}
             };
             InitRoom.ForEach(s => context.Rooms.Add(s));
             context.SaveChanges();

             var InitBox = new List<Box>()
             {
                 new Box{BoxId=0, RoomNumber=0,Items=null}
             };
             InitBox.ForEach(s => context.Box.Add(s));
             context.SaveChanges();


             var InitItem = new List<Item>()
             {
                 new Item{BoxID=0, ItemName="", Quantity=0}
             };
             InitItem.ForEach(s => context.Item.Add(s));
             context.SaveChanges();
         }
     }
 }

这是我的 web.config 文件中的代码:

   <entityFramework>
        <contexts>
             <context type="WhatsInStorage.DAL.DatabaseContext, WhatsInStorage">
                  <databaseInitializer type="WhatsInStorage.DAL.DatabaseInitializer, WhatsInStorage" />
             </context>
       </contexts>
   </entityFramework>
   <connectionStrings>
        <add name="DatabaseContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=WhatsInStorage1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
   </connectionStrings>

关于我哪里出错的任何想法? 谢谢!

您在创建初始化程序 class 的位置有错字。 这里: DatabaseInitalizer

还不能发表评论,但你能分享你的Rooms class 吗?

另外,我认为 EF 不允许您在将密钥 ID 添加到数据库时指定Key ID 所以尽量只添加没有ID的数据。

例子:

var InitItem = new List<Item>()
{
    new Item{ItemName="", Quantity=0}
};

InitItem.ForEach(s => context.Item.Add(s));
context.SaveChanges();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM