无法确定关系的主要结束。多个添加的实体可以具有相同的主键。

问题描述:

 

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

        public string TownName
        {
            get;
            set;
        }

        ObservableCollection<Building> buildings;

        [ForeignKey("TownID")]
        public ObservableCollection<Building> Buildings
        {
            get
            {
                if (buildings == null)
                    buildings = new ObservableCollection<Building>();

                return buildings;
            }
            set
            {
                buildings = value;
            }
        }
    }


    public class Building
    {
        public int BuildingID
        {
            get;
            set;
        }

        public string BuildingName
        {
            get;
            set;
        }

        public int TownID
        {
            get;
            set;
        }

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

        ObservableCollection<Worker> workers;

        [ForeignKey("BuildingID")]
        public ObservableCollection<Worker> Workers
        {
            get
            {
                if (workers == null)
                    workers = new ObservableCollection<Worker>();

                return workers;
            }
            set
            {
                workers = value;
            }
        }
    }


    public class Worker
    {
        public int WorkerID
        {
            get;
            set;
        }

        public string WorkerName
        {
            get;
            set;
        }

        public int BuildingID
        {
            get;
            set;
        }

        [ForeignKey("BuildingID")]
        public Building Building
        {
            get;
            set;
        }
    }


        static void Main(string[] args)
        {
            Model.CityCatalog cat = new Model.CityCatalog();

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

            int iBuildings = 0;
            while (iBuildings < 2)
            {
                Building b = new Building()
                {
                    BuildingName = "Town Center" + (++iBuildings).ToString(),
                    Town = MyTown
                };

                cat.Buildings.Add(b);

                int iWorkers = 0;
                while (iWorkers < 2)
                {
                    Worker w = new Worker()
                    {
                        WorkerName = "Worker " + (++iWorkers).ToString(),
                        Building = b
                    };

                    cat.Workers.Add(w);
                }

            }
            cat.SaveChanges();
        }

执行上述公司de给出错误

Executing the above code gives error

无法确定'CodeFirstNamespace.Building_Workers'关系的主要结尾。多个添加的实体可能具有相同的主键。

Unable to determine the principal end of the 'CodeFirstNamespace.Building_Workers' relationship. Multiple added entities may have the same primary key.

 如果在Building循环中执行cat.SaveChanges(),则可以很好地保存数据库。

 If you execute cat.SaveChanges() inside Building loop it saves fine to the database.

 但是,保存多个建筑物会出现上述错误。

 but, saving multiple Buildings gives above error.

我认为这是在集合导航属性上指定[ForeignKey]属性的副作用。您可能最终会遇到多种关系,而您只需要一种关系。您可以尝试从集合中删除属性吗?
查看问题是否已解决?

I think this is a side effect of specifying [ForeignKey] attributes on the collection navigation properties. You are probably ending up with multiple relationships where you only really want one. Can you try removing the attributes from the collections and see if the issue is resolved?

~Rowan