Entity Framework一对一流畅Api无法删除数据

Entity Framework One to One Fluent Api Can not Delete Data

那就是模型中的礼物class。那应该是父 class。

public class Gift
    {
        public int GiftId { get; set; }           
        public string Title { get; set; }
        public string Brand { get; set; }
        public double Price { get; set; }
        public bool Chosen { get; set; }   
        public virtual Shop Shop { get; set; }    
        public virtual Person Person { get; set; }        
    }

也就是店铺class,这两个是一对一的关系。有礼必有店,有店必有礼

public class Shop
    {
        public int ShopId { get; set; }
        public string Name { get; set; }
        public string Street { get; set; }
        public string Number { get; set; }
        public string Postcode { get; set; }
        public string District { get; set; }  
        public virtual Gift Gift { get; set; }
    }

这是我模型中的第三个 class。这个class和礼物class是一对零的关系。如果没有选择礼物,则没有任何人。人也一样。

 public class Person
{
    public int Id { get; set; }
    public string FirstName{ get; set; }
    public string Surname{ get; set; }
    public string EmailAdress { get; set; }
    public virtual Gift Gift { get; set; }
}

这是我改过很多次的流利api。

modelBuilder.Entity<Gift>()
                .HasOptional(x => x.Person);

            modelBuilder.Entity<Person>()
                .HasRequired(x => x.Gift);

            modelBuilder.Entity<Gift>()
            .HasRequired(x => x.Shop).WithOptional(x => x.Gift).Map(x => x.MapKey("ShopId"));

            modelBuilder.Entity<Shop>()
               .HasRequired(x => x.Gift).WithOptional(x => x.Shop).Map(x => x.MapKey("GiftId"));

我可以保存数据,但是当我想删除礼物时,我无法成功并且遇到问题。我该如何解决?已经谢谢了!

我已经修好了。 Here is the link

modelBuilder.Entity<Shop>()
                .HasRequired(x => x.Gift)
                .WithRequiredDependent();
            modelBuilder.Entity<Gift>()
                .HasRequired(x => x.Shop)
                .WithRequiredPrincipal();
            base.OnModelCreating(modelBuilder);