Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,14 @@ public InternalEntityEntry PrepareToSave()

throw new InvalidOperationException(CoreStrings.UnknownKeyValue(entityType.DisplayName(), property.Name));
}

if (property.GetElementType() != null
&& !property.IsNullable
&& GetCurrentValue(property) == null)
{
throw new InvalidOperationException(
CoreStrings.NullRequiredPrimitiveCollection(EntityType.DisplayName(), property.Name));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also throw for null complex properties

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, but needs a bit of extra work first to stop it throwing from the change tracker.

}
}
else if (EntityState == EntityState.Modified)
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,9 @@
<data name="NullableKey" xml:space="preserve">
<value>A key on entity type '{entityType}' cannot contain the property '{property}' because it is nullable/optional. All properties on which a key is declared must be marked as non-nullable/required.</value>
</data>
<data name="NullRequiredPrimitiveCollection" xml:space="preserve">
<value>The primitive collection property '{type}.{property}' is configured as required (non-nullable) but has a null value when saving changes. Either mark the property as optional (nullable) or set a non-null value.</value>
</data>
<data name="ObjectRemovedFromModel" xml:space="preserve">
<value>The object has been removed from the model.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Microsoft.EntityFrameworkCore.TestModels.JsonQuery;
Expand Down Expand Up @@ -73,7 +74,10 @@ public IList<bool> TestBooleanCollection
}

public byte[] TestByteCollection { get; set; }

[Required]
public List<Guid> TestGuidCollection { get; set; }

public IList<ushort> TestUnsignedInt16Collection { get; set; }
public uint[] TestUnsignedInt32Collection { get; set; }
public ObservableCollection<ulong> TestUnsignedInt64Collection { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,21 @@ public virtual Task Edit_single_property_relational_collection_of_nullable_enum_
Assert.False(result.NewCollectionSet);
});

[ConditionalFact]
public virtual async Task SaveChanges_throws_when_required_primitive_collection_is_null()
=> await TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var entity = new JsonEntityAllTypes { TestGuidCollection = null };
context.Add(entity);

Assert.Equal(
CoreStrings.NullRequiredPrimitiveCollection(nameof(JsonEntityAllTypes), nameof(JsonEntityAllTypes.TestGuidCollection)),
(await Assert.ThrowsAsync<InvalidOperationException>(async () => await context.SaveChangesAsync())).Message);
});

public void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());

Expand Down