Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Issue: DbGeography that has no keys defined. [650]

$
0
0
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: To be clear, I am using .net 4.5. Here is my repro code: This is my entity class which has a DbGeography prop 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(); } } And I have DataAccess Reopository class which inherits from DbContext public class DbContextRepository : DbContext, IRepository { public DbContextRepository() { } public DbContextRepository(string connectionStringName) : base(connectionStringName) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { GeoSuburbEntity.SetupDataModel(modelBuilder); } } At the app starts I run db auto migration like below: private static void AutoMigrateDatabase(string connectionName) { var dbConfiguration = new DatabaseMigrationConfiguration() { TargetDatabase = new DbConnectionInfo(connectionName) }; var migrator = new DbMigrator(dbConfiguration); migrator.Update(); } And the DatabaseMigrationConfiguration class def is like: public sealed class DatabaseMigrationConfiguration : DbMigrationsConfiguration<DbContextRepository> { public DatabaseMigrationConfiguration() { AutomaticMigrationDataLossAllowed = true; //DEV only AutomaticMigrationsEnabled = true; } protected override void Seed(DbContextRepository context) { } } Once I run the app, it throws that error

Viewing all articles
Browse latest Browse all 10318

Trending Articles