无法确定关系的主要结束。多个添加的实体可以具有相同的主键。 [英] Unable to determine the principal end of the relationship. Multiple added entities may have the same primary key.

查看:59
本文介绍了无法确定关系的主要结束。多个添加的实体可以具有相同的主键。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

    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


这篇关于无法确定关系的主要结束。多个添加的实体可以具有相同的主键。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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