MVC3 EF在多个表上发布

本文关键字:EF MVC3 | 更新日期: 2023-09-27 18:23:38

我正在开发一个包含3个表的数据库

跳闸模式:

    [Key]
    public int TripId { get; set; }
    [ForeignKey("Driver")]
    public int DriverId { get; set; }
    public virtual Driver Driver { get; set; }
    public string StartingPoint { get; set; }
    public string Destination { get; set; }
    public DateTime TimeDepart { get; set; }
    public int SeatAvailable { get; set; }
    public virtual ICollection<Driver> Drivers { get; set; }
    public virtual ICollection<Passenger> Passengers { get; set; }

驱动程序型号:

    [Key]
    public int DriverId { get; set; }
    public string DriverName { get; set; }
    [ForeignKey("Trip"), Column(Order = 0)]
    public int TripId { get; set; }
    public virtual Trip Trip { get; set; }

最后一位乘客型号:

    [Key]
    public int PassengerId { get; set; }
    public string PassengerName { get; set; }
    [ForeignKey("Trip"), Column(Order = 1)]
    public int TripId { get; set; }
    public virtual Trip Trip { get; set; }

带有:

    public class LiveGreenContext: DbContext
    {
        public DbSet<Trip> Trips { get; set; }
        public DbSet<Driver> Drivers { get; set; }
        public DbSet<Passenger> Passengers { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    }

我得到以下错误:

无法检查模型兼容性,因为EdmMetadata类型为未包含在模型中。确保IncludeMetadataConvention具有已添加到DbModelBuilder约定中。

这个问题有什么解决方案吗?谢谢

MVC3 EF在多个表上发布

尝试在Global.asax:的Application_Start事件处理程序中添加对Database.SetInitializer方法的调用

Database.SetInitializer<ContextName>(null);

其中ContextName是自定义DbContext类的名称。