2014-01-22 15 views
11

Ich benutze zuerst EF6-Code, um meine db zu erstellen. Alles war gut letzte Nacht arbeiten, jetzt, wenn ich update-Datenbank Befehl ausführen, bekomme ich die folgende Ausnahme:Der Wert darf nicht null sein. Parametername: extent

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 

Hier ist der Kontext Klasse

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

Und hier ist die Konfiguration:

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


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

    } 
} 

Ich habe im Internet gesucht und gefunden, dass ich nur Eigenschaften in meiner Kontextklasse haben sollte. Und ich habe dreimal geprüft, es hat nur dbset Eigenschaften. Ich kann nicht herausfinden, was passiert ist. Jede Hilfe würde sehr geschätzt werden. Vielen Dank.

+0

Wie sieht Ihre Kontextklasse aus und wie sieht die Entität aus, die Sie zuletzt geändert haben? – Rob

+0

Bitte posten Sie Ihre 'DbModelBuilder' Konfiguration. Scheint, als ob eine Relation Option nicht gesetzt ist – oxfn

+0

Bitte beachten Sie die Änderung. Außerdem habe ich versucht, den Fluent-API-Code zu kommentieren. Aber wieder kein Erfolg. –

Antwort

29

Das Problem gefunden. Eigentlich hatte ich eine HttpPosetedFileBase Eigenschaft im UserInfo-Modell, die die EF nicht an die Datenbankspalte binden konnte.

Durch das Setzen eines nicht zugeordneten Attributs funktionierte es.

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

Also, für andere, die dieses Problem sicher zwei Dinge erreichen:

  1. Ihr Kontext sollte immer DbSets Eigenschaften enthalten. Beispiel -

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

  2. Es soll keinen Typen in Ihren Modellklassen, die EF nicht auf eine Datenbankspalte binden.

Ich hoffe, es hilft.

+0

Ist mir passiert, indem ich versucht habe, eine Spalte basierend auf 'System.IO.Stream' zu erstellen, als ich'byte [] 'verwenden sollte – bithavoc

+0

Vielen Dank für diese Antwort, speziell # 2. Hatte eine Klasse mit einer MD5-Eigenschaft, seit Tagen gesucht, warum dies brach. –

+0

Ich hatte das gleiche Problem, nachdem ich alle Entitäten durchgesehen hatte, konnte ich keines der erwähnten Probleme finden. Dann habe ich Visual Studio Express 2012 neu gestartet, und das hat das Problem gelöst? :) – smerlung

Verwandte Themen