为什么我在 .NET 6 MVC 中为我的预订类创建迁移时遇到外键冲突?

问题描述 投票:0回答:0

我在 .net6 mvc 上构建了一个预订系统。然而,当我创建我的预订班级并进行迁移时,我收到了这个错误。

外键属性“Reservation.ClientID1”是在影子状态下创建的,因为实体类型中存在具有简单名称“ClientID”的冲突属性,但未映射,已用于其他关系,或者与关联的主键类型。有关 EF Core 中映射关系的信息,请参阅 https://aka.ms/efcore-relationships

我遵循的做法与我在整个过程中所做的相同,没有任何问题。希望有人能帮助我。我正在尝试创建的所有 5 个外键都得到了这个。

    public class Reservation
    {
        public int ID { get; set; }
        public string ReservationNumber { get; set; }
        public string Name { get; set; }
        public int ReservedPax { get; set; }
        public int Comps { get; set; }
        public int? ActualPax { get; set; }
        public string SpecialRequests { get; set; }
        public string AlcoholRequests { get; set; }
        public string Comments { get; set; }

        public int MenuID { get; set; }
        public virtual Menu Menu { get; set; }

        public int StatusID { get; set; }
        public virtual Status Status { get; set; }

        public int ContactID { get; set; }
        public virtual Contact Contact { get; set; }


        public int RestaurantID { get; set; }
        public virtual Restaurant Restaurant { get; set; }

        public int ClientID { get; set; }
        public virtual Client Client { get; set; }

        public virtual ICollection<ReservationMenuItem> ReservationMenuItems { get; set; } = new HashSet<ReservationMenuItem>();
            modelBuilder.Entity<Reservation>()
                .HasOne(r => r.Contact)
                .WithMany()
                .HasForeignKey(r => r.ContactID)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Reservation>()
                .HasOne(r => r.Menu)
                .WithMany()
                .HasForeignKey(r => r.MenuID)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Reservation>()
                .HasOne(r => r.Status)
                .WithMany()
                .HasForeignKey(r => r.StatusID)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Reservation>()
                .HasOne(r => r.Client)
                .WithMany()
                .HasForeignKey(r => r.ClientID)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Reservation>()
                .HasOne(r => r.Restaurant)
                .WithMany()
                .HasForeignKey(r => r.RestaurantID)
                .OnDelete(DeleteBehavior.Restrict);

我尝试过更具体地改变上下文,但这没有用

c# asp.net-mvc model-view-controller .net-6.0
© www.soinside.com 2019 - 2024. All rights reserved.