简体   繁体   English

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

[英]'Update-Database' in EF 4.3 code-first - Value cannot be null

Using asp.NET MVC3, I am creating trying to create an abstract class/model with the following: 使用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
}

Which is used by: 使用者:

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



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

When I run the update-database in Entity Framework 4.3 code first migrations I get: 当我在Entity Framework 4.3代码中运行update-database ,我得到的第一次迁移:

Value cannot be null.
Parameter name: key

Am I doing something wrong? 难道我做错了什么? Is this even an accepted way of doing things.I don't really want to use an interface as this would mean implementing all the fields in each thing that uses item 这甚至是一种可接受的做事方式。我真的不想使用界面,因为这意味着实现每个使用item东西的所有字段

EDIT I have just found that this comes from the following code in global.asax.cs in Application_Start: 编辑我刚刚发现这来自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
        }
      }

    }

Core COntext: 核心COntext:

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

Your error is coming from public Type ConcreteType , are you missing a class definition? 您的错误来自public Type ConcreteType ,您是否缺少类定义? Type normally refers to System.Type is that what you're actually wanting to put in your class? 类型通常是指System.Type是你实际想要放在你班级的那个? EF has no way to translate that into a SQL type. EF无法将其转换为SQL类型。

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

相关问题 实体框架代码优先迁移因更新数据库而失败 - Entity Framework code-first migration fails with update-database 值不能为空。 (参数&#39;o&#39;)更新数据库上的EF Core - Value cannot be null. (Parameter 'o') EF Core On Update-Database EF6代码优先:更新数据库登录失败 - EF6 Code First: Login Failing on update-database EF代码优先-从Azure门户运行更新数据库 - EF Code First - run Update-Database from Azure portal 更新数据库上的ASP.NET代码优先(实体框架)错误 - ASP.NET Code-First (Entity Framework) Error on update-database 实体框架代码优先:使用&#39;Update-Database&#39;生成SQL脚本在解析时会产生XML错误 - Entity Framework Code-First: Generate SQL script with 'Update-Database' produces XML error while parsing 实体框架代码优先:迁移失败,更新数据库,强制不必要的(?)添加迁移 - Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration 无法在.NET Core中使用EF更新数据库 - Cannot Update-Database with EF in .NET Core 空导航在EF 5代码优先 - Null Navigation In EF 5 code-first EF代码优先多对多更新 - EF code-first Many to Many Update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM