C# EF代码优先:每种混凝土类型的表
我尝试了以下代码,但我得到以下错误,有人有想法吗 错误3013:映射从第17行开始的片段时出现问题:缺少表映射:外键约束“CountryBase\u Cities1”从表Cities CountryId到表CountryBase Id:没有为表CountryBase指定映射。\r\n} 参考链接:C# EF代码优先:每种混凝土类型的表,c#,.net,entity-framework-4,linq-to-entities,entity-framework-4.1,C#,.net,Entity Framework 4,Linq To Entities,Entity Framework 4.1,我尝试了以下代码,但我得到以下错误,有人有想法吗 错误3013:映射从第17行开始的片段时出现问题:缺少表映射:外键约束“CountryBase\u Cities1”从表Cities CountryId到表CountryBase Id:没有为表CountryBase指定映射。\r\n} 参考链接: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D
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; }
}
}