外键问题?

Foreign Key Issue?

在我的 HomeController 中,我的创建函数访问数据库时遇到问题。在我的浏览器中提交表单后,这是显示的错误:

给定错误

MySqlException: 无法添加或更新子行:外键约束失败 (petshelterdb.pets, CONSTRAINT FK_Pets_Owners_OwnerId FOREIGN KEY (OwnerId) REFERENCES owners (OwnerId) 删除级联) MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) 在 ResultSet.cs,第 49 行

我没有使用 login/registration。我的想法是我有一个“公告板”,显示可以领养的宠物和可以领养的主人。可以将新宠物或主人添加到板上。如果我 select 主人的名字,我可以让那个主人“领养”一只宠物。我在 HomeController 代码中指定了哪一行是问题所在。

由于我没有使用 UserId,所以我不确定该怎么做。

Pet.cs

namespace petShelter.Models
{
    public class Pet
    {
        [Key]
        public int PetId { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Type { get; set; }

        [Required]
        public string Description { get; set; }

        public string Skill1 { get; set; }
        public string Skill2 { get; set; }
        public string Skill3 { get; set; }


        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }

        public Owner Owner { get; set; }
        public int OwnerId { get; set; }

    }
}

Owner.cs

namespace petShelter.Models
{
    public class Owner
    {
        [Key]
        public int OwnerId { get; set; }

        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }

        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }


        List<Pet> MyPets { get; set; }

        public string FullName()
        {
            return FirstName + " " + LastName;
        }

    }
}

HomeController.cs

 [HttpPost("/pet/new")]
        public IActionResult Create(Pet newPet)
        {
            if (ModelState.IsValid)
            {
                _context.Add(newPet);
                _context.SaveChanges(); ***THIS IS WHERE ISSUE OCCURS***
                return RedirectToAction("Index", new { id = newPet.PetId });
            }
            else
            {
                if (newPet.Name == null)
                {
                    ModelState.TryAddModelError("Name", "The Name field is required");
                }
                return View("NewPet", newPet);
            }
        }

PetShelterContext.cs

namespace petShelter.Models
{
    public class PetShelterContext : DbContext
    {
        public PetShelterContext(DbContextOptions options) : base(options) { }

        public DbSet<Pet> Pets { get; set; }
        public DbSet<Owner> Owners { get; set; }

    }
}

替换

public int OwnerId { get; set; }

public int? OwnerId { get; set; }

并修复属性(这些属性对于 net5 是可选的)

public class Owner
    {
        .......

        [InverseProperty(nameof(Pet.Owner))]
        public virtual ICollection<Pet> Pets { get; set; }
       
        .....
}

也许这里也有

public class Pet
    {
        .....

   public int OwnerId { get; set; }

   [ForeignKey(nameof(OwnerId))]
    [InverseProperty("Pets")]
   public Owner Owner { get; set; }
     
    }