值不能为空。参数名: extent。

12

我正在使用EF6的Code First来创建我的数据库。昨晚一切都很顺利,但是当我运行update-database命令时,出现了以下异常:

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.ArgumentNullException: Value cannot be null.
Parameter name: extent
   at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
   at System.Data.Entity.Core.Mapping.StorageEntitySetMapping..ctor(EntitySet extent, StorageEntityContainerMapping entityContainerMapping)
   at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.AddEntitySetMapping(DbDatabaseMapping databaseMapping, EntitySet entitySet)
   at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping)
   at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping)
   at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Value cannot be null.
Parameter name: extent

这里是上下文类(Context Class)。
public class WheelTrackDb : DbContext
{
    public WheelTrackDb(): base("DefaultConnection"){ }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Driver>()
            .HasRequired(d => d.Vehicle)
            .WithOptional(v => v.Driver)
            .Map(m => m.MapKey("VehicleId"));

        modelBuilder.Entity<TransactionLog>()
           .HasRequired(t => t.Acceptor)
           .WithMany()
           .HasForeignKey(t => t.AcceptorId)
           .WillCascadeOnDelete(false);

        modelBuilder.Entity<TransactionLog>()
           .HasRequired(t => t.Donor)
           .WithMany()
           .HasForeignKey(t => t.DonorId)
           .WillCascadeOnDelete(false);

        modelBuilder.Entity<Violation>()
            .HasRequired(v => v.GSMDeviceLog)
            .WithMany(g => g.Violations)
            .HasForeignKey(v => v.GSMDeviceLogId);

        modelBuilder.Entity<RouteFence>().Ignore(r => r.PolygonVertices);

        modelBuilder.Entity<Vehicle>()
            .HasRequired(v => v.License)
            .WithRequiredPrincipal();

        modelBuilder.Entity<MessageCounter>()
            .HasRequired(m => m.License)
            .WithOptional(l => l.MessageCounter)
            .Map(m => m.MapKey("LicenseId"));

        modelBuilder.Entity<GSMDeviceLog>()
            .HasRequired(g => g.Vehicle)
            .WithMany(t => t.GsmDeviceLogs);

    }

    public DbSet<UserInfo> UserInfos{ get; set; }
    public DbSet<TransactionLog> TransactionLogs{ get; set; }
    public DbSet<Driver> Drivers { get; set; }
    public DbSet<Vehicle> Vehicles{ get; set; }
    public DbSet<VehicleGroup> VehicleGroups { get; set; }
    public DbSet<License> Licenses { get; set; }
    public DbSet<Policy> Policys { get; set; }
    public DbSet<GSMDeviceLog> GSMDeviceLogs { get; set; }
    public DbSet<MessageCounter> MessageCounters { get; set; }
    public DbSet<Violation> Violations { get; set; }
    public DbSet<OwnershipPaper> OwnershipPapers { get; set; }
    public DbSet<Subscriber> Subscribers { get; set; }
}

以下是配置信息:

internal sealed class Configuration : DbMigrationsConfiguration<WheelTrack.Models.WheelTrackDb>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }


    protected override void Seed(WheelTrack.Models.WheelTrackDb context)
    {

    }
}

我在网上搜索并发现我的上下文类中应该只有属性,而且我检查了三遍,它只有dbset属性。我无法弄清楚发生了什么。任何帮助将不胜感激。谢谢。


你的上下文类是什么样子的?你最后修改的实体是什么样子的? - Rob
请发布您的 DbModelBuilder 配置。似乎某些关系选项未设置。 - oxfn
请查看编辑。我也尝试对流畅API代码进行注释,但仍然没有成功。 - Ashish Charan
1个回答

30

找到了问题。实际上,在UserInfo模型中我有一个HttpPostedFileBase属性,但EF无法将其绑定到数据库列。

在它上面加一个not-mapped属性使它起作用了。

    [NotMapped]
    public HttpPostedFileBase Photo { get; set; }

因此,对于遇到此问题的其他人,请确保两件事情:

  1. 您的上下文应始终包含DbSet属性。例如 -

    public DbSet<Policy> Policies { get; set; }

  2. 您的模型类中不应存在EF无法绑定到数据库列的类型。

希望能有所帮助。


з”ұдәҺжҲ‘е°қиҜ•еҹәдәҺВҙSystem.IO.StreamВҙеҲӣе»әеҲ—иҖҢдёҚжҳҜдҪҝз”ЁВҙbyte[]ВҙпјҢиҝҷз§Қжғ…еҶөеҸ‘з”ҹдәҶгҖӮ - bithavoc
感谢您提供的答案,特别是#2。我有一个带有MD5属性的类,已经搜索了好几天,想知道为什么它会出现问题。 - Joshua Robinson
我也曾经遇到同样的问题,在查阅所有实体后,我没有发现任何上述问题。然后我重新启动了 Visual Studio Express 2012,这个问题就解决了? :) - smerlung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接