MVC3 EF 在多个表上出现问题

发布于 2025-01-03 17:37:13 字数 1676 浏览 3 评论 0原文

我正在开发一个包含 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2025-01-10 17:37:13

尝试添加对 Database.SetInitializerGlobal.asax 的 Application_Start 事件处理程序中的 a> 方法:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文