Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top