I am hitting ModelValidationException when I use CodeFirst/DbModelBuilder to build my data model. One of my entities has DbGeography property. As long as I run the app, I will get the below exception:
One or more validation errors were detected during model generation:
\thttp://schemas.microsoft.com/ado/2009/11/edm.DbGeography: : EntityType 'DbGeography' has no key defined. Define the key for this EntityType.
I tried EF 5 and EF 6 apha1 and had the same issue.
Comments: I am not able to repro this. This is the code I am running namespace ConsoleApplication3 { public class GeoSuburbEntity { public int Id { get; set; } public string Name { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public DbGeography Location { get; set; } public string State { get; set; } public string Postcode { get; set; } public string Timezone { get; set; } public float UtcOffset { get; set; } public static void SetupDataModel(DbModelBuilder builder) { builder.Entity<GeoSuburbEntity>().ToTable("GeoSuburb"); builder.Entity<GeoSuburbEntity>().HasKey(x => x.Id); builder.Entity<GeoSuburbEntity>().Property(x => x.Name).HasMaxLength(250).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Latitude).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Longitude).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.State).HasMaxLength(100).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Postcode).HasMaxLength(50).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Timezone).HasMaxLength(25).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.UtcOffset).IsRequired(); } } public class DbContextRepository : DbContext //, IRepository { public DbContextRepository() { } public DbContextRepository(string connectionStringName) : base(connectionStringName) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { GeoSuburbEntity.SetupDataModel(modelBuilder); } } class Program { private static void AutoMigrateDatabase(string connectionName) { var dbConfiguration = new DatabaseMigrationConfiguration() { TargetDatabase = new DbConnectionInfo(connectionName) }; var migrator = new DbMigrator(dbConfiguration); migrator.Update(); } static void Main(string[] args) { AutoMigrateDatabase("Test"); using(var ctx = new DbContextRepository()) { ctx.Set<GeoSuburbEntity>().Add( new GeoSuburbEntity() { Id = 0, Latitude = 0, Location = DbSpatialServices.Default.GeographyFromText("POINT(0 0)"), Longitude = 0, Name = "Test", Postcode = "Test", State = "Test", Timezone = "Test", UtcOffset = 0 }); ctx.SaveChanges(); var x = ctx.Set<GeoSuburbEntity>().FirstOrDefault(); } } } } Can you provide the stack trace? Can you attach your repro project to the bug?
One or more validation errors were detected during model generation:
\thttp://schemas.microsoft.com/ado/2009/11/edm.DbGeography: : EntityType 'DbGeography' has no key defined. Define the key for this EntityType.
I tried EF 5 and EF 6 apha1 and had the same issue.
Comments: I am not able to repro this. This is the code I am running namespace ConsoleApplication3 { public class GeoSuburbEntity { public int Id { get; set; } public string Name { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public DbGeography Location { get; set; } public string State { get; set; } public string Postcode { get; set; } public string Timezone { get; set; } public float UtcOffset { get; set; } public static void SetupDataModel(DbModelBuilder builder) { builder.Entity<GeoSuburbEntity>().ToTable("GeoSuburb"); builder.Entity<GeoSuburbEntity>().HasKey(x => x.Id); builder.Entity<GeoSuburbEntity>().Property(x => x.Name).HasMaxLength(250).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Latitude).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Longitude).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.State).HasMaxLength(100).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Postcode).HasMaxLength(50).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.Timezone).HasMaxLength(25).IsRequired(); builder.Entity<GeoSuburbEntity>().Property(x => x.UtcOffset).IsRequired(); } } public class DbContextRepository : DbContext //, IRepository { public DbContextRepository() { } public DbContextRepository(string connectionStringName) : base(connectionStringName) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { GeoSuburbEntity.SetupDataModel(modelBuilder); } } class Program { private static void AutoMigrateDatabase(string connectionName) { var dbConfiguration = new DatabaseMigrationConfiguration() { TargetDatabase = new DbConnectionInfo(connectionName) }; var migrator = new DbMigrator(dbConfiguration); migrator.Update(); } static void Main(string[] args) { AutoMigrateDatabase("Test"); using(var ctx = new DbContextRepository()) { ctx.Set<GeoSuburbEntity>().Add( new GeoSuburbEntity() { Id = 0, Latitude = 0, Location = DbSpatialServices.Default.GeographyFromText("POINT(0 0)"), Longitude = 0, Name = "Test", Postcode = "Test", State = "Test", Timezone = "Test", UtcOffset = 0 }); ctx.SaveChanges(); var x = ctx.Set<GeoSuburbEntity>().FirstOrDefault(); } } } } Can you provide the stack trace? Can you attach your repro project to the bug?