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

以下方法失败,提交时出现异常:

 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’关系.多个添加的实体可能具有相同的主键.

当拆分提交并插入Methodos时,它会起作用:

  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一次.为什么在第一个例子中失败,这个例外意味着什么?

推荐答案

错误是由无法解决的外键ID(与引用相反)引起的.在您的例子中,有一个LocationInRole引用了ID为0的位置.有多个位置具有此ID.

这些位置尚未分配ID,因为它们尚未保存到数据库中,即生成ID时.在您的第二个示例中,位置在它们的ID被访问之前被保存,这就是为什么这是工作的原因.

如果只想稍后保存更改,则无法依赖位置ID来定义关系.

换下一行...

LocationId = locations[i].Id

.为了这个.

Location = locations[i]

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

.net相关问答推荐

Msbuild try 构建 msbuild.exe 而不是我的 .csproj 文件

.NET 中两个子字符串之间的非贪婪正则表达式匹配

如何在 C# 中自动删除临时文件?

在.NET C#中截断整个单词的字符串

图像 UriSource 和数据绑定

将 DataRowCollection 转换为 IEnumerable

在 C# DllImport 中使用 32 位或 64 位 dll

泛型方法是如何、何时、何地具体化的?

使用多个 MemoryCache 实例

如何在不丢失数据的情况下重命名 Entity Framework 5 Code First 迁移中的数据库列?

如何将枚举值序列化为 int?

如何从 .NET 中的流中获取 MemoryStream?

如何制作通用类型转换函数

等效于 C# 在 powershell 中的使用关键字?

IronPython 与 Python .NET

是 C# 中的 bool 读/写原子

obj 文件夹是为了什么而生成的?

/langversion 的错误选项6无效;必须是 ISO-1、ISO-2、3、4、5 或默认值

如何为我的 C# 应用程序创建产品密钥?

如何将两个 List 相互比较?