我在一個網站上作為一個學習工具作業,并且遇到了物體框架無法設定資料庫初始化程式的問題。這是我得到的錯誤:
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'.
為了創建我的控制器,我使用物體框架添加了一個帶有視圖的 MVC5 控制器的新控制器。作為模型類,我使用了 Box(它顯示在下拉串列中),對于資料背景關系類,我使用了 DatabaseContext(預填充)。
這是模型 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; }
}
}
這是模型 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>
關于我哪里出錯的任何想法?謝謝!
uj5u.com熱心網友回復:
您在創建初始化程式類的地方有一個錯字。這里:DatabaseInitalizer
uj5u.com熱心網友回復:
還不能發表評論,但你能分享你的Rooms
課程嗎?
另外,我認為 EF 不允許您Key ID
在將其添加到資料庫時指定。所以盡量只添加沒有ID
.
例子:
var InitItem = new List<Item>()
{
new Item{ItemName="", Quantity=0}
};
InitItem.ForEach(s => context.Item.Add(s));
context.SaveChanges();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504925.html
標籤:C# asp.net-mvc 实体框架 4