1

This question is identical to this question and this question and closely related to this question. However, none of the answers to any of those questions worked for me.

Everything compiles fine, but when the database is initialized, I get the following exception, even though the [Key] attribute is in there.

Additional information: One or more validation errors were detected during model generation:

DataLayer.Vehicle: : EntityType 'Vehicle' has no key defined. Define the key for this EntityType.

Vehicle: EntityType: EntitySet 'Vehicles' is based on type 'Vehicle' that has no keys defined.

The relevant classes:

[Table("Vehicles")]
public class Vehicle
{
    [Key] public uint Vin { get; set; }
    public string Type { get; set; }
    public virtual Route CurrentRoute { get; set; }
    public string Plate { get; set; } 
    public virtual List<TrackPointRaw> TrackPointsRaw { get; set; }
    public virtual List<TrackPoint> TrackPoints { get; set; }

    public Vehicle(uint vin)
    {
        Vin = vin;
    }
}

...

public partial class VehicleTrackingContext : DbContext
{
    public DbSet<Vehicle> Vehicles { get; set; }
    public DbSet<TrackPointRaw> TrackPointsRaw { get; set; }
    public DbSet<TrackPoint> TrackPoints { get; set; }
    public DbSet<Route> Routes { get; set; }
}

Using statements are already present for both DataAnnotations and DataAnnotations.Schema.

I'd like to avoid calling the Vin VinId since that's horribly redundant, though doing so hasn't helped anyway. (N.B. VIN is converted to a uint elsewhere; that's not the problem.)

I'm doing code-first and I'm trying to avoid migrations. The database is only really being used to cache data from within one run of the application and doesn't need to store anything persistently. Thus, at the application startup, I'm calling:

using (var vtc = new VehicleTrackingContext())
{
    vtc.Database.CreateIfNotExists();
    vtc.Database.Initialize(true);
}

The exception is thrown on vtc.Database.Initialize(true);

I'm using LocalDb at the moment; developing in VS Ultimate 2013, targeting .NET 4.5.

Community
  • 1
  • 1
James K.
  • 364
  • 1
  • 15

0 Answers0