views:

114

answers:

1

I am having problems trying to map an existing legacy database. In this example a Country has many Cities.

The problem is the foreign key on the City table is not the primary key of the Country table

Table Definitions

CREATE TABLE [dbo].[CD_Country](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Code] [char](2) NOT NULL,
 [Name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_CD_Country] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[CD_City](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Code] [char](3) NOT NULL,
 [Name] [nvarchar](50) NOT NULL,
 [CountryCode] [char](2) NOT NULL,
 CONSTRAINT [PK_CD_City] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Im trying do something like this but I get exception.

"The mapping of navigation property 'Country' to join table 'CD_Country' is not valid. Each key property of the type 'Test_SOLEF.City' must be included exactly once in the mapping."

public static class Test
{
    public static void DoIt()
    {
        using (TestEFContext context = new TestEFContext())
        {
            foreach (Country country in context.Coutries.ToList())
            {
                Console.WriteLine(string.Format("{0} has {1} cities.", country.Name, country.Cities.Count));
            }
        }
    }
}

public class TestEFContext : DbContext
{
    public IDbSet<Country> Coutries { get; set; }
    public IDbSet<City> Cities { get; set; }

    public TestEFContext()
    {
        ObjectContext.ContextOptions.LazyLoadingEnabled = true;
    }

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        // city
        modelBuilder.Entity<City>()
            .HasKey(p => p.Id)
            .MapSingleType()
            .ToTable("CD_City");

        modelBuilder.Entity<City>()
            .Property(c => c.Id).IsIdentity();

        modelBuilder.Entity<City>()
            .HasOptional(c => c.Country)
            .WithMany(c => c.Cities)
            .Map(
                "CD_Country",
                (city, country) => new 
                {
                    CountryCode = city.CountryCode,
                    Code = country.Code
                });

        // country
        modelBuilder.Entity<Country>()
            .HasKey(p => p.Id)
            .MapSingleType()
            .ToTable("CD_Country");

    }

}

public class City
{
    public int Id {get;set;}
    public string Code {get;set;}
    public string Name {get;set;}
    public string CountryCode {get;set;}

    public virtual Country Country {get;set;}
}

public class Country
{
    public int Id {get;set;}
    public string Code {get;set;}
    public string Name {get;set;}

    public virtual ICollection<City> Cities {get;set;}
}
A: 

Official reply from Microsoft

"FKs to to non-primary keys is something that is not currently supported by the Entity Framework, but it is something we are working on.

Thanks, Arthur"

Forum post http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/56eedb7b-3e2a-4a2b-bd62-d451753bd9b5/#903991f6-7c5e-4aa4-a560-ef9826f8c660

Adam

related questions