Skip to content

Bad error message : "Nullable object must have a value" when required field not populated #109

@Terebi42

Description

@Terebi42

Given the following models (and importantly, the configuration)

public class EffortEntity
	{
		[Key]
		public Guid Id { get; set; }
		public Guid? ChildId { get; set; }
		public virtual EffortEntityChild Child { get; set; }

	}

	public class EffortEntityChild
	{
		[Key]
		public Guid Id { get; set; }
	}

	internal class EntityConfig : EntityTypeConfiguration<EffortEntity>
	{
		public EntityConfig()
		{
			HasRequired(a => a.Child)
				.WithMany()
				.HasForeignKey(c => c.ChildId);
		}
	}

The following unit test will fail

               var connection = Effort.DbConnectionFactory.CreateTransient();
		var context = new EffortContext(connection, true);

			var entity = new EffortEntity()
			{
				Id = Guid.NewGuid()
			};

			context.Entities.Add(entity);
			context.SaveChanges();

But with no indication of which column or even table is causing the problem, or that the problem is at all a missing required field.


Test method EffortNullableTest.UnitTest1.TestMethod1 threw exception: 
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Nullable object must have a value.
    at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at lambda_method(Closure )
--- End of inner exception stack trace ---
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Effort.Internal.CommandActions.InsertCommandAction.CreateAndInsertEntity(ITable table, IList`1 memberBindings, Transaction transaction)
   at Effort.Internal.CommandActions.InsertCommandAction.ExecuteNonQuery(ActionContext context)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
--- End of inner exception stack trace ---
    at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
    at System.Data.Entity.Internal.InternalContext.SaveChanges()

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions