C# 无法检索元数据,多重性在实体框架中不是有效角色
是否需要在fluent API中指定映射所需的所有关系? 我的模型里有四节课。具体来说:它们是=>公共汽车、乘客、路线、预订C# 无法检索元数据,多重性在实体框架中不是有效角色,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,是否需要在fluent API中指定映射所需的所有关系? 我的模型里有四节课。具体来说:它们是=>公共汽车、乘客、路线、预订 public class Bus { [Key] public int Id { get; set; } [Display(Name="Route")] public int RouteId { get; set; } .............. [ForeignKey("RouteId")] public v
public class Bus
{
[Key]
public int Id { get; set; }
[Display(Name="Route")]
public int RouteId { get; set; }
..............
[ForeignKey("RouteId")]
public virtual Route Route{ get; set; }
public virtual ICollection<Reservation> Reservations { get; set; }
}
public class Passenger
{
public int Id { get; set; }
.........
.........
public virtual Reservation Reservation { get; set; }
}
public class Reservation
{
public int Id { get; set; }
public int BusId { get; set; }
public int PassengerId { get; set; }
.................
..............
[ForeignKey("BusId")]
public virtual Bus Bus { get; set; }
[ForeignKey("PassengerId")]
public virtual Passenger Passenger { get; set; }
}
public class Route
{
public int Id { get; set; }
.............
.............
public virtual ICollection<Bus> Busses { get; set; }
}
公共类总线
{
[关键]
公共int Id{get;set;}
[显示(Name=“路线”)]
公共int路由ID{get;set;}
..............
[外键(“路由ID”)]
公共虚拟路由{get;set;}
公共虚拟ICollection保留{get;set;}
}
公务舱乘客
{
公共int Id{get;set;}
.........
.........
公共虚拟保留{get;set;}
}
公务舱预订
{
公共int Id{get;set;}
公共int BusId{get;set;}
公共int PassengerId{get;set;}
.................
..............
[外键(“BusId”)]
公共虚拟总线{get;set;}
[外键(“PassengerId”)]
公共虚拟乘客{get;set;}
}
公务舱路线
{
公共int Id{get;set;}
.............
.............
公共虚拟ICollection总线{get;set;}
}
我已将我的DbContext文件建模如下:
public class TicketContext: DbContext
{
public TicketContext()
: base("TicketDbContext")
{
}
public DbSet<Bus> Busses { get; set; }
public DbSet<Passenger> Passengers { get; set; }
public DbSet<Reservation> Reservations { get; set; }
public DbSet<Route> Routes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Code First to ignore PluralizingTableName convention
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//configure the primary key for "Bus" ENTITY
modelBuilder.Entity<Bus>()
.HasKey(p=>p.Id);
//configure one to many relationship between "Bus" and "Passenger" ENTITY
modelBuilder.Entity<Bus>()
.HasMany(p => p.Reservations)
.WithRequired(p => p.Bus);
//configure the primary key for "Passenger" ENTITY
modelBuilder.Entity<Passenger>()
.HasKey(p => p.Id);
//configure one to one relationship between "Passenger" and "Reservation" ENTITY
modelBuilder.Entity<Passenger>()
.HasRequired(i => i.Reservation)
.WithRequiredPrincipal(i => i.Passenger);
//configure the primary key for "Reservation" ENTITY
modelBuilder.Entity<Reservation>()
.HasKey(p => p.Id);
//configure the primary key for "Route" ENTITY
modelBuilder.Entity<Route>()
.HasKey(p => p.Id);
//configure one to many relationship between "Route" and "Bus" ENTITY
modelBuilder.Entity<Route>()
.HasMany(r => r.Busses)
.WithOptional(r => r.Route);
}
}
公共类TicketContext:DbContext
{
公共票务上下文()
:base(“TicketDbContext”)
{
}
公共数据库集总线{get;set;}
公共数据集乘客{get;set;}
公共数据库集保留{get;set;}
公共数据库集路由{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//首先配置代码以忽略PluralizingTableName约定
modelBuilder.Conventions.Remove();
//为“总线”实体配置主键
modelBuilder.Entity()
.HasKey(p=>p.Id);
//配置“总线”和“乘客”实体之间的一对多关系
modelBuilder.Entity()
.HasMany(p=>p.Reservations)
.WithRequired(p=>p.Bus);
//为“乘客”实体配置主键
modelBuilder.Entity()
.HasKey(p=>p.Id);
//配置“乘客”和“预订”实体之间的一对一关系
modelBuilder.Entity()
.has必需(i=>i.Reservation)
.具有所需的委托人(i=>i.乘客);
//为“保留”实体配置主键
modelBuilder.Entity()
.HasKey(p=>p.Id);
//为“路由”实体配置主键
modelBuilder.Entity()
.HasKey(p=>p.Id);
//配置“路由”和“总线”实体之间的一对多关系
modelBuilder.Entity()
.HasMany(r=>r.Busses)
.可选(r=>r.路线);
}
}
之后,当我想使用模型类Bus和指定的DbContext为总线创建控制器时,VisualStudio给出了以下错误。
我做错了什么?是否必须为所有实体指定关系