我尝试了以下代码,但出现以下错误,有人知道吗?
错误 3013:从第 17 行开始映射片段时出现问题:缺少表映射:从表 Cities (CountryId) 到表 CountryBase (Id) 的外键约束“CountryBase_Cities1”:未为表 CountryBase 指定映射。\r\n"}
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; }
}
}