多表上的MVC3 EF问题(MVC3 EF issues on multiple table)

编程入门 行业动态 更新时间:2024-10-23 06:30:58
表上的MVC3 EF问题(MVC3 EF issues on multiple table)

我正在开发一个包含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约定中。

关于这个问题的解决方案? 谢谢!

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!

最满意答案

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

Database.SetInitializer<ContextName>(null);

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

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.

更多推荐

本文发布于:2023-08-06 20:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表上   EF   table   multiple   issues

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!