Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架一对一Fluent Api无法删除数据_C#_Entity Framework_Fluent Nhibernate - Fatal编程技术网

C# 实体框架一对一Fluent Api无法删除数据

C# 实体框架一对一Fluent Api无法删除数据,c#,entity-framework,fluent-nhibernate,C#,Entity Framework,Fluent Nhibernate,这是模型中的礼物类。这应该是父类 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

这是模型中的礼物类。这应该是父类

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; }        
    }
这是Shop类,这两个类有一对一的关系。礼物应该有商店,商店应该有礼物

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; }
    }
这是我模型中的第三个类。该类与礼品类具有一对零的关系。如果礼物没有被选择,它就没有任何人。这个人也一样

 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; }
}
这是我多次修改的fluentapi

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"));
modelBuilder.Entity()
.has可选(x=>x.Person);
modelBuilder.Entity()
.HasRequired(x=>x.Gift);
modelBuilder.Entity()
.HasRequired(x=>x.Shop).WithOptional(x=>x.Gift).Map(x=>x.MapKey(“ShopId”);
modelBuilder.Entity()
.HasRequired(x=>x.Gift)。with可选(x=>x.Shop).Map(x=>x.MapKey(“GiftId”);
我可以保存数据,但当我想删除礼物时,我无法成功,并且出现问题。我怎样才能解决这个问题?唐克斯已经死了

我已经修好了

modelBuilder.Entity()
.HasRequired(x=>x.Gift)
.WithRequiredDependent();
modelBuilder.Entity()
.has必需(x=>x.Shop)
.WithRequiredPrincipal();
基于模型创建(modelBuilder);
modelBuilder.Entity<Shop>()
                .HasRequired(x => x.Gift)
                .WithRequiredDependent();
            modelBuilder.Entity<Gift>()
                .HasRequired(x => x.Shop)
                .WithRequiredPrincipal();
            base.OnModelCreating(modelBuilder);