前述のように、次のようList<System.Drawing.Color>
に使用される を持つモデルがあります。Seed()
protected override void Seed(DatabaseContext context)
{
var somethings = new List<Something>
{
new Something
{
Name="blah blah", Colors= { Color.Black, Color.Red }
}
};
}
そして、私がそこにいる限り、Colors
私はいつも
Object reference not set to an instance of an object.
ラインで受け取りvar somethings = new List<Something>
ます.
を削除するとColors
、それはなくなり、すべてが完全に機能します。何が原因で、この問題をどのように解決できますか?
ありがとう。
編集:
Something
のモデル:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
namespace MVCApplication7
{
public class Something
{
public int SomethingID { get; set; }
public string Name { get; set; }
public List<Color> Colors { get; set; }
}
}
コントローラ:
........
private DatabaseContext db = new DatabaseContext();
//
// GET: /Home/
public ViewResult Index()
{
return View(db.Somethings.ToList());
}
表示: (デバッガーがColors
が空であることを示しているため、無関係であると確信しています。
@foreach (var itemColor in item.Colors)
{
Html.Raw(itemColor.ToString());
}
Global.asax
.........
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Database.SetInitializer<DatabaseContext>(new DatabaseInitializer());
}