EF Code First:每种混凝土类型的表格

时间:2012-06-18 14:54:06

标签: c# .net entity-framework-4 linq-to-entities entity-framework-4.1

我尝试了以下代码,但我得到了以下错误,有没有人有想法?

错误3013:从第17行开始映射片段时出现问题:缺少表映射:从表Cities(CountryId)到表CountryBase(Id)的外键约束'CountryBase_Cities1':没有为表CountryBase指定映射。\ r \ n “}

参考链接:http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Migrations;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var db = new Context();
            db.Database.Delete();
            db.Database.Create();
            db.SaveChanges();
            db.Cities.ToList();

        }
    }




    public class Context : DbContext
    {
        public DbSet<CityBase> Cities { get; set; }
        public DbSet<CountryBase> Countries { get; set; }
        public DbSet<Area> Areas { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<CountryBase>()
          .Property(p => p.Id)
          .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);


            modelBuilder.Entity<CityBase>()
           .Property(p => p.Id)
           .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);


            modelBuilder.Entity<City>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Cities");
            });

            modelBuilder.Entity<Country>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Countries");
            });


            base.OnModelCreating(modelBuilder);
        }


    }

    public abstract class Entity : IUniqueable
    {
        [Key]
        public virtual int Id { get; set; }

    }

    public  abstract class CityBase :Entity
    {
        public int CountryId { get; set; }
        public string Code { get; set; }
        public bool Active { get; set; }
        public CountryBase Country { get; set; }

    }

    public abstract class CountryBase : Entity
    {

        public string Code { get; set; }
        public bool Active { get; set; }
        public virtual ICollection<CityBase> Cities { get; set; }

    }

    public class City : CityBase
    {
        public int PointX { get; set; }
        public int PointY { get; set; }
        public virtual ICollection<Area> Areas { get; set; }
    }

    public class Country : CountryBase
    {

        public int PointX { get; set; }
        public int PointY { get; set; }

    }

    public class Area:Entity
    {
        public string Name { get; set; }
        public City City { get; set; }
    }


    public interface IUniqueable
    {
        int Id { get; set; }
    }
}

0 个答案:

没有答案