3

我已经从 nuget 获取了 Entity Framework Extended 源代码,我想使用它的 delete 表达式来使用 ID 删除多行数据。

在我的代码中,我使用 PersonID 来获取 ClaimsID。在两个表中,ClaimsID 是一个 FK,我从中删除的最后一个表是 PK。由于关键约束,我按此顺序执行此操作。

出于某种原因,它给了我一个空引用:

public void DeleteClaims(string _PersonID)
        {
            Guid PersonID = Guid.Parse(_PersonID);
            try
            {
                using (CasaLatinaEntities CasaLatinaEntities = new CasaLatinaEntities())
                {
                    //Delete all FKs first before deleting parent data 
                    var tblClaims = CasaLatinaEntities.tblClaims.Where(c => c.PersonID == PersonID);
                    List<Guid> IDs = tblClaims.Select(c => c.ClaimID).ToList();

                    foreach (var ID in IDs)
                    {
                        var ClaimAction = from c in CasaLatinaEntities.tblClaimActions
                                          where c.ClaimID == ID
                                          select c;

                        var ClaimTypes = from c in CasaLatinaEntities.tblClaimTypes
                                         where c.ClaimID == ID
                                         select c;


                        if (ClaimAction.Count() > 0)
                        {
                            CasaLatinaEntities.tblClaimActions.Delete(c => c.ClaimID == ID);
                            CasaLatinaEntities.tblClaimTypes.Delete(c => c.ClaimID == ID);
                            CasaLatinaEntities.tblClaims.Delete(c => c.ClaimID == ID);
                        }
                        else if (ClaimTypes.Count() > 0)
                        {
                            CasaLatinaEntities.tblClaimTypes.Delete(c => c.ClaimID == ID);
                            CasaLatinaEntities.tblClaims.Delete(c => c.ClaimID == ID);
                        }
                        else
                        {
                            CasaLatinaEntities.tblClaims.Delete(c => c.ClaimID == ID);
                        }
                    }

                    //CasaLatinaEntities.tblClaimActions.Delete(c => c.ClaimID == ClaimId);
                    //CasaLatinaEntities.tblClaimTypes.Delete(c => c.ClaimID == ClaimId);
                    //CasaLatinaEntities.tblClaims.Delete(c => c.ClaimID == ClaimId);
                }
            }
            catch (Exception ex)
            {

            }

        }

此行代码中断

CasaLatinaEntities.tblClaimActions.Delete(c => c.ClaimID == ClaimId);

我尝试注释掉这段代码,但每次删除尝试都会中断。

堆栈跟踪

   at EntityFramework.Mapping.ReflectionMappingProvider.SetProperties(EntityMap entityMap, Object mappingFragmentProxy)
   at CallSite.Target(Closure , CallSite , Type , EntityMap , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at EntityFramework.Mapping.ReflectionMappingProvider.CreateEntityMap[TEntity](ObjectQuery query)
   at EntityFramework.Mapping.ReflectionMappingProvider.GetEntityMap[TEntity](ObjectQuery query)
   at EntityFramework.Mapping.MappingResolver.GetEntityMap[TEntity](ObjectQuery query)
   at EntityFramework.Extensions.BatchExtensions.Delete[TEntity](IQueryable`1 source)
   at EntityFramework.Extensions.BatchExtensions.Delete[TEntity](IQueryable`1 source, Expression`1 filterExpression)
   at DataLayer.RepositoryClient.DeleteClaims(String _PersonID) in c:\Users\nickgowdy\Documents\Visual Studio 2012\Projects\CasaLatina\DataLayer\RepositoryClient.cs:line 876

这不是很多代码,所以我不明白空引用的用途。数据行存在于所有三个表中。

4

2 回答 2

2

最新的 Entity Framework Extended 是为 Entity Framework 6.0 版编写的。您正在尝试将它用于 Entity Framework 6.1,它是几天前发布的,但尚不支持。

现在尝试使用 Remove()/RemoveRange(),直到他们制作 EF:Extended 的固定版本。

于 2014-03-20T14:25:10.203 回答
0

由于 FirstOrDefault 不能返回任何记录,您可能需要在继续之前先检查它:

//Delete all FKs first before deleting parent data 
var tblClaims = CasaLatinaEntities.tblClaims.Where(c => c.PersonID == PersonID);
Guid ClaimId = tblClaims.Select(c => c.ClaimID).FirstOrDefault();

if (ClaimId != null)
{
   CasaLatinaEntities.tblClaimActions.Delete(c => c.ClaimID == ClaimId);
   CasaLatinaEntities.tblClaimTypes.Delete(c => c.ClaimID == ClaimId);
   CasaLatinaEntities.tblClaims.Delete(c => c.ClaimID == ClaimId);
}
于 2014-03-19T13:34:36.127 回答