此问题与this question和this question相同,与this question密切相关。但是,这些问题的答案都不适合我。
Everything编译得很好,但是在初始化数据库时,我得到以下异常,即使[Key]属性在那里。
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.
相关课程:
[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; }
}
DataAnnotations和DataAnnotations.Schema已经存在使用语句。
我想避免打电话给Vin VinId,因为那是非常多余的,尽管这样做无论如何都没有帮助。 (N.B. VIN在其他地方转换为uint;这不是问题。)
我正在做代码优先,我正在努力避免迁移。该数据库实际上仅用于从应用程序的一次运行中缓存数据,并且不需要持久存储任何内容。因此,在应用程序启动时,我正在调用:
using (var vtc = new VehicleTrackingContext())
{
vtc.Database.CreateIfNotExists();
vtc.Database.Initialize(true);
}
在vtc.Database.Initialize(true);
我现在正在使用LocalDb;在VS Ultimate 2013中开发,面向.NET 4.5。