'Update-Database' in EF 4.3 code-first - V

2019-08-17 18:55发布

问题:

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; }
}

回答1:

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.