多个添加实体可以具有相同的主键 [英] Multiple added entities may have the same primary key

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

问题描述

下面是我的3个实体模型:路线,地点和LocationInRoute

Here is my model of 3 entities: Route, Location and LocationInRoute.

下面的方法将失败,并得到异常时,它提交:

the following method fails and get exception when commit it:

 public static Route InsertRouteIfNotExists(Guid companyId, IListLocation> locations)
        {
            //Loop on locations and insert it without commit
            InsertLocations(companyId, routesOrLocations);

            RouteRepository routeRep = new RouteRepository();
            Route route = routeRep.FindRoute(companyId, locations);
            if (route == null)
            {
                route = new Route()
                {
                    CompanyId = companyId,
                    IsDeleted = false
                };
                routeRep.Insert(route);
                LocationInRouteRepository locInRouteRep = new LocationInRouteRepository();
                for (int i = 0; i < locations.Count; i++)
                {
                    locInRouteRep.Insert(new LocationInRoute()
                    {
                        //Id = i,
                        LocationId = locations[i].Id,
                        Order = i,
                        RouteId = route.Id
                    });
                }
            }
            return route;
        }

在这样做的:

InsertRouteIfNotExists(companyId, locations);
UnitOfWork.Commit();

我:

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

在分裂提交并插入到methos - 它的工作原理:

When splitting the commit and insert in into the methos - it works:

  public static Route InsertRouteIfNotExists(Guid companyId, IListLocation> locations)
            {
                //Loop on locations and insert it without commit
                InsertLocations(companyId, routesOrLocations);
                UnitOfWork.Commit();

                RouteRepository routeRep = new RouteRepository();
                Route route = routeRep.FindRoute(companyId, locations);
                if (route == null)
                {
                    route = new Route()
                    {
                        CompanyId = companyId,
                        IsDeleted = false
                    };
                    routeRep.Insert(route);
                    LocationInRouteRepository locInRouteRep = new LocationInRouteRepository();
                    for (int i = 0; i < locations.Count; i++)
                    {
                        locInRouteRep.Insert(new LocationInRoute()
                        {
                            //Id = i,
                            LocationId = locations[i].Id,
                            Order = i,
                            RouteId = route.Id
                        });
                    }
                    UnitOfWork.Commit();
                }
                return route;
            }

我想调用commit一次和方法之外。失败的原因在第一个例子,什么此异常意思?

I would like to call commit once and outside the method. Why it fails in the first example and what does this exception means?

推荐答案

该错误是由一个外键ID(相对于基准),不能得到解决。你的情况,你有一个LocationInRole引用一个位置为0。有多个地点与此ID的ID。

The error is caused by a foreign key ID (as opposed to a reference) which cannot be resolved. In your case, you have a LocationInRole that references a Location with an ID of 0. There are multiple Locations with this ID.

的位置还没有被分配的ID,因为他们尚未保存到其生成的ID时的数据库。在第二个例子中,位置在保存之前它们的ID来访问这就是为什么这个工程。

The Locations have not yet been assigned an ID because they have not yet been saved to the database which is when the ID is generated. In your second example, the Locations are saved before their IDs are accessed which is why this works.

您将无法靠上的位置标识,如果你想调用SaveChanges后来才确定的关系。

You will not be able to rely on the Location IDs to define the relationships if you want to SaveChanges only later.

交换以下行...

LocationId = locations[i].Id

...这个...

...for this...

Location = locations[i]

的关系将然后可以基于对象引用这是不依赖于LocationIDs

The relationships will then be based on object references which are not dependent on the LocationIDs.

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

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