The first if condition runs just fine, the second fails with a 'cannot cast from guid to string' error. The compiler tells me that both Ids are guids. So, why is this failing on the 2nd 'if'? l.P.Id is a guid, testP.Id is also a guid. I don't see how there's a failure here.
if (context.GetSet<PBM>().FirstOrDefault(l=>l.P.Id == testP.Id) != null)
{
context.GetSet<PBM>()
.Remove(context.GetSet<PBM>()
.FirstOrDefault(l => l.P.Id == testP.Id));
context.SaveChanges();
}
if (context.GetSet<MOAH>().FirstOrDefault(l=>l.P.Id == testP.Id) != null)
{
context.GetSet<MOAH>()
.Remove(context.GetSet<MOAH>()
.FirstOrDefault(l => l.P.Id == testP.Id));
context.SaveChanges();
}
After using .Any(l=>l.P.Id == testP.Id)
as suggested, now all my unit tests fail with the exception System.InvalidCastException : Invalid cast from 'System.String' to 'System.Guid'.
Error stack trace as requested:
System.InvalidCastException : Invalid cast from 'System.String' to 'System.Guid'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type targetType)
at MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling(Int32 ordinal)
at lambda_method(Closure, Shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source)
at System.Linq.Queryable.FirstOrDefault(IQueryable`1 source, Expression`1 predicate)