EF 4.3代码优先的“更新 - 数据库” - 值不能为空

时间:2012-08-10 12:44:53

标签: c# asp.net-mvc-3 entity-framework ef-code-first ef-migrations

使用asp.NET MVC3,我正在尝试使用以下内容创建一个抽象类/模型:

namespace core.Models.Concrete
{
  public class item
  {
    [Key]
    public Guid id { get; set; }
    public Type ConcreteType { get; set; }
    public itemtype Type { get; set; }
    public string barcode { get; set; }
    public int rating { get; set; }
    public string Title { get; set; }
  }
}


public enum itemtype 
{
  Game,
  Book,
  Film
}

使用者:

namespace core.Models.Abstract
{
  public class game : item
  {
    public gamePlatform platform { get; set; }
  }



  public enum gamePlatform
  {
    PC,
    X360,
    PS3,
    Wii
  }
}

当我在Entity Framework 4.3代码中运行update-database时,我得到了第一次迁移:

Value cannot be null.
Parameter name: key

我做错了吗?这甚至是一种可接受的做事方式。我真的不想使用界面,因为这意味着实现使用item

的每个事物中的所有字段

修改 我刚刚发现这来自Application_Start中的global.asax.cs中的以下代码:

protected void Application_Start()
    {
      AreaRegistration.RegisterAllAreas();

      RegisterGlobalFilters(GlobalFilters.Filters);
      RegisterRoutes(RouteTable.Routes);

      //System.Data.Entity.Database.SetInitializer<DataContext>(new DataContextInitializer());

      using (coreContext TestContext = new coreContext())
      {
        var x =TestContext.Users.Count();  <!-- THIS LINE HERE
        try
        {
          Roles.CreateRole("User");
        }catch
        {
          //Already created
        }
      }

    }

核心COntext:

public class coreContext : DbContext
    {
 public DbSet<core.Models.Abstract.game> games { get; set; }
}

1 个答案:

答案 0 :(得分:2)

您的错误来自public Type ConcreteType,您是否缺少类定义?类型通常是指System.Type你实际想要在课堂上放什么? EF无法将其转换为SQL类型。