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

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

问题描述

这是3个实体的模型:Route,Location和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();

我有:


无法确定SimTaskModel.FK_T_STF_SUB_LOCATION_IN_ROUTE_T_STF_LOCATION_location_id关系的主体端。多个添加的实体可能具有相同的主键。

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引用一个ID为0的位置。有多个这个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.

您将无法依靠位置ID来定义关系如果你想稍后保存更改。

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

...为此...

Location = locations[i]

然后,关系将基于不依赖于LocationID的对象引用。

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

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

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