0
public class BasePage
{        
    public int Id { get; set; }
    ...
}

public class Region : BasePage
{        
    public virtual MapCoordinates Map { get; set; }
    ...
}

public class Place: BasePage
{        
    public virtual MapCoordinates Map { get; set; }
    ...
}

public class MapCoordinates
{
    [Key, ForeignKey("BasePage")]
    public int Id { get; set; }

    public virtual BasePage BasePage { get; set; }
    ...
}

Getting this exception on SaveChanges:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

Wonder
  • 994
  • 3
  • 10
  • 23

1 Answers1

0

use this code :

    public class BasePage
    {        
        public int Id { get; set; }

        public virtual MapCoordinate Map { get; set; }
        ...
    }

    public class Region : BasePage
    {                   
        ...
    }

    public class Place: BasePage
    {        
        ...
    }

    public class MapCoordinate
    {
        [Key]
        public int BasePageId { get; set; }
        ...
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BasePage>()
                    .HasRequired(e => e.MapCoordinate)
                    .WithRequiredPrincipal();

        modelBuilder.Entity<BasePage>().ToTable(BasePages);
        modelBuilder.Entity<MapCoordinate>().ToTable(MapCoordinates);
    }

check out these links :

Associations in EF Code First: Part 4 – Table Splitting

Associations in EF Code First: Part 5 – One-to-One Foreign Key Associations

Code First and Fluent API one to one relationship

Community
  • 1
  • 1
Iraj
  • 1,492
  • 5
  • 18
  • 42
  • I don't want BasePage to have Map, only several derived classes should have it. – Wonder Feb 19 '14 at 18:29
  • you want have table splittings? – Iraj Feb 19 '14 at 18:37
  • I keep BasePage with it's derived classes (Region, Place etc) in one table, and Map in another one. – Wonder Feb 19 '14 at 18:58
  • ok , I think in MapCoordinate navigate to region and place instead of BasePage and you couldn't use table splitted between basePage and MapCoordinate , then in MapCoordinate you must have two foreign key, One for region and one for place. – Iraj Feb 19 '14 at 19:18
  • yeah, that probably will work, but looks like ugly solution, coz in fact I don't need two foreign keys. – Wonder Feb 19 '14 at 19:29
  • yes of course ,but if you use navigation in drived class instead base class , you forced use 2 foreign key, the course i agree using table splitted between Basepage and MapCoordinate. – Iraj Feb 19 '14 at 19:37