EF 4.3中的“更新数据库” - 首先 - 值不能为空 [英] 'Update-Database' in EF 4.3 code-first - Value cannot be null

查看:327
本文介绍了EF 4.3中的“更新数据库” - 首先 - 值不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 命名空间核心.Models.Concrete 
{
public class item
{
[Key]
public Guid id {get;组; }
public Type ConcreteType {get;组; }
public itemtype Type {get;组; }
public string barcode {get;组; }
public int rating {get;组; }
public string标题{get;组; }
}
}


public枚举itemtype
{
游戏
书,
电影

使用哪个:

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



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

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

 值不能为空。 
参数名称:key

我做错了什么?这甚至是一种可接受的做事方式。我不想使用界面,因为这将意味着在每个使用项目的东西中实现所有的字段



编辑
我刚刚发现这是来自 global.asax.cs 在Application_Start中:

  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(); <! - 此行
尝试
{
Roles.CreateRole(User);
} catch
{
//已经创建
}
}

}
/ pre>

Core COntext:

  public class coreContext:DbContext 
{
public DbSet< core.Models.Abstract.game>游戏{get;组; }
}


解决方案

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


Using asp.NET MVC3, I am creating trying to create an abstract class/model with the following:

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:

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

EDIT I have just found that this comes from the following code in global.asax.cs in Application_Start:

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:

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? Type normally refers to System.Type is that what you're actually wanting to put in your class? EF has no way to translate that into a SQL type.

这篇关于EF 4.3中的“更新数据库” - 首先 - 值不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆