Search code examples
c#linqasp.net-mvc-3entity-framework-4entity-framework-4.1

MVC3 EF issues on multiple table


i am developing a database with 3 tables

Trip Model:

    [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; }

Driver model:

    [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; }

And last passenger model:

    [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; }

With:

    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>();
    }

And i get the following error:

Model compatibility cannot be checked because the EdmMetadata type was not included in the model. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.

Any solutions on this issue? Thanks!


Solution

  • Try adding a call to the Database.SetInitializer method in the Application_Start event handler of your Global.asax:

    Database.SetInitializer<ContextName>(null);
    

    where ContextName is the name of your custom DbContext class.