删除Master Detail场景中的记录,其中Master和Detail模型都实现了TPT .. Referential Integrity Constraint错误。 [英] Deleting records in Master Detail scenario where both Master and Detail models both implements TPT.. Referential Integrity Constraint error.

查看:50
本文介绍了删除Master Detail场景中的记录,其中Master和Detail模型都实现了TPT .. Referential Integrity Constraint错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

      public    class   城镇 
     {
         public   int   TownID
         {
          &NBSP ;  get ;
             设置;
        }

          public   string   TownName
   ;       {
             get ;
             set ;
        }

       ;   ObservableCollection < House >  houses;

         [ ForeignKey " TownID" )]         public   ObservableCollection < 房子> 房子                         get
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
                 if  (房屋  ==  null
                      houses  =  new   ObservableCollection < House >();

         ;         return   houses;
             }
             设置
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;    houses  =  value ;
            ; }        }
    }


< pre style ="font-family:consolas">     public   abstract   class   Property
     {
         public   int &NBSP;物业ID
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;  get ;
             设置;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP ;   } 


      public    class    House  :  财产 
     {
         公开  字符串  HouseAddress
         {
             get ;
  ;            set ;
     ;    }
         public   ; int   TownID
         {
             get ;
  ;            set ;
     ;    }
         [ ForeignKey " TownID" )]         公开  城镇 城镇         {
             get ;
              set ;
         }来自        ObservableCollection < 房间> 房间;

     ;     [ ForeignKey " HouseID" )]
         public   ObservableCollection < 房间> 房间         {
  ;            get
     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;  if  (rooms  ==  null
    ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N bsp;       rooms  =  new   ObservableCollection < 房间>();

          ;        return   rooms;
             }
             设置
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;           rooms  =  value ;
   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;   }


  ;     public    class   区域 
     {
         公开  int   AreaID
         {
             get ;
              set ;
         }    }


     < span style ="color:blue"> public    class   房间  ;:  区域 
     {
     &nb sp;   public   string   RoomName
     ;      {
             获取;
             set ;
        }

         公开  int   HouseID
         {
             get ;
  ;            set ;
     ;    }

         [ ForeignKey " HouseID" )]          public   House   House
          {
             get ;
             set ;        }
    }

以下是模型构建器声明

          protected   覆盖   void   OnModelCreating(模型构建器&NBSP;模型构建器)
&NBSP;&NBSP;&NBSP;&NBSP;&N bsp;    {
             base .OnModelCreating(modelBuilder);

             modelBuilder.Entity< Property >()。ToTable(" Properties" );
              modelBuilder.Entity< House >()。ToTable(" Houses" );             modelBuilder.Entity< 建筑物>()。ToTable("建筑物");

              modelBuilder.Entity< Area >()。ToTable(< span style ="color:#a315 15">"区域"
);             modelBuilder.Entity< 房间>()。ToTable(" Rooms" );
     ;    }

执行以下DeleteRecords方法会出错

 
         private   static   void   DeleteRecords()
         {
             Model。 CityCatalog   cat  =  < span style ="color:blue"> new   Model。 CityCatalog ();

    ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cat.Towns.Loa d();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cat.Houses.Load();
&NBSP;&NBSP;&NBSP;           cat.Rooms.Load();

              城镇  MyTown  =  cat.Towns.Take(1).First();

             foreach  (  House   h    MyTown.Houses.ToList())
    ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;     foreach  (会议室  r  &NBSP; h.Rooms.ToList())
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N BSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; h.Rooms.Remove(R);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cat.Rooms.Remove(R);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;     }
                &NBSP; MyTown.Houses.Remove(H);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cat.Houses 。删除(h);
            }

     ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; cat.SaveChanges();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;

DELETE sta与REFERENCE约束"FK_Rooms_Houses"冲突的tement。冲突发生在数据库"OZTowns",表"dbo.Rooms",列'HouseID'。

该声明已被终止。


我已经重新检查了几次,并没有发现我想要做的任何事情。


 


NeoTech

解决方案


这可能与集合属性上的"[ForeignKey]属性"有关。问题出现在你的其他几个主题中。


你可以尝试删除它们吗?如果你仍然有问题发布代码,我会看看。


~Rowan


 

    public class Town
    {
        public int TownID
        {
            get;
            set;
        }

        public string TownName
        {
            get;
            set;
        }

        ObservableCollection<House> houses;

        [ForeignKey("TownID")]
        public ObservableCollection<House> Houses
        {
            get
            {
                if (houses == null)
                    houses = new ObservableCollection<House>();

                return houses;
            }
            set
            {
                houses = value;
            }
        }
    }

    public abstract class Property
    {
        public int PropertyID
        {
            get;
            set;
        }
    } 

    public class House : Property
    {
        public string HouseAddress
        {
            get;
            set;
        }

        public int TownID
        {
            get;
            set;
        }

        [ForeignKey("TownID")]
        public Town Town
        {
            get;
            set;
        }


        ObservableCollection<Room> rooms;

        [ForeignKey("HouseID")]
        public ObservableCollection<Room> Rooms
        {
            get
            {
                if (rooms == null)
                    rooms = new ObservableCollection<Room>();

                return rooms;
            }
            set
            {
                rooms = value;
            }
        }
    }

    public class Area
    {
        public int AreaID
        {
            get;
            set;
        }
    }

    public class Room : Area
    {
        public string RoomName
        {
            get;
            set;
        }

        public int HouseID
        {
            get;
            set;
        }

        [ForeignKey("HouseID")]
        public House House
        {
            get;
            set;
        }
    }

Below are the model builder declarations

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Property>().ToTable("Properties");
            modelBuilder.Entity<House>().ToTable("Houses");
            modelBuilder.Entity<Building>().ToTable("Buildings");

            modelBuilder.Entity<Area>().ToTable("Areas");
            modelBuilder.Entity<Room>().ToTable("Rooms");
        }

Executing the below DeleteRecords method gives error


        private static void DeleteRecords()
        {
            Model.CityCatalog cat = new Model.CityCatalog();

            cat.Towns.Load();
            cat.Houses.Load();
            cat.Rooms.Load();

            Town MyTown = cat.Towns.Take(1).First();

            foreach ( House h in MyTown.Houses.ToList())
            {
                foreach (Room r in h.Rooms.ToList())
                {
                    h.Rooms.Remove(r);
                    cat.Rooms.Remove(r);
                }

                MyTown.Houses.Remove(h);
                cat.Houses.Remove(h);
            }

            cat.SaveChanges();
        } 

The DELETE statement conflicted with the REFERENCE constraint "FK_Rooms_Houses". The conflict occurred in database "OZTowns", table "dbo.Rooms", column 'HouseID'.
The statement has been terminated.

I have rechecked a couple of times and don't find anything wrong in what i am trying to do.

 


NeoTech

解决方案

Hi,

This may be related to the "[ForeignKey] attribute on collection properties" issue that has come up in a couple of your other threads.

Can you try removing them and if you still have issues post up the code and I will take a look.

~Rowan


这篇关于删除Master Detail场景中的记录,其中Master和Detail模型都实现了TPT .. Referential Integrity Constraint错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆