实体框架扩展 - 简单删除给我空引用

时间:2014-03-19 12:45:33

标签: linq c#-4.0 entity-framework-extended

我已经从nuget获取了Entity Framework Extended源代码,我想使用它删除表达式来使用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

它没有很多代码,所以我不明白空引用是什么。所有三个表中都存在数据行。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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);
}