3

I've grabbed the Entity Framework Extended source code from nuget and I want to use it's delete expression to delete multiple rows of data using ID.

In my code I use PersonID to get ClaimsID. In two tables ClaimsID is an FK and the last table I delete from it is PK. I am doing it in this order because of key constraint.

For some reason it is giving me a null reference:

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)
            {

            }

        }

Code breaks at this line

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

I tried annotating this code out but it breaks at every delete attempt.

Stack trace

   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

It's not alot of code so I don't understand what the null reference is for. The rows of data exist in all three tables.

nick gowdy
  • 6,191
  • 25
  • 88
  • 157
  • at which line exactly the null reference exception was thrown? – har07 Mar 19 '14 at 12:54
  • @har07 CasaLatinaEntities.tblClaimActions.Delete(c => c.ClaimID == ClaimId);. My context contains data. – nick gowdy Mar 19 '14 at 12:55
  • Can you check `ClaimId` variable value when exception thrown? and is there any data match that value in your table? I'm not experienced with EF extended library, this is my best guess : EF extended throws exception if there is no data to be deleted (there is no data match criteria). – har07 Mar 19 '14 at 13:06
  • Let me guess... you try to use EF 6.1 with the EF 6.0 Extension Tools, right? I'm asking because I do, and I got the exact same exception. – Kornél Regius Mar 20 '14 at 14:18
  • It appears that your question is correctly answered by @KornélRegius. Please remember to accept an answer if it satisfies your question! – Avi Cherry Aug 05 '14 at 20:57

2 Answers2

2

The latest Entity Framework Extended is written for Entity Framework version 6.0. You are trying to use it for Entity Framework 6.1, which came out a few days ago and is not supported yet.

Try using Remove()/RemoveRange() for now, until they make a fixed version of EF:Extended.

Kornél Regius
  • 2,989
  • 5
  • 30
  • 39
0

Since FirstOrDefault can return no records, you might want to check it first before proceeding:

//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);
}
Jim Wooley
  • 10,169
  • 1
  • 25
  • 43
  • Hey I've changed my code so I am deleting when there are rows to delete. In my old code FirstOrDefault() isn't good because one person can have many claims. I've changed it but I still get the same error. ClaimID is does contain a value. – nick gowdy Mar 19 '14 at 14:12