question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Many-to-many relationship - ArgumentNullException: Value cannot be null. (Parameter 'name')

See original GitHub issue

Describe the bug ArgumentNullException is thrown when entities have a many-to-many relationship.

To Reproduce Here is a repository with a ready-to-run project that demonstrates the issue: AuditExample

We have two entities: Person and Department that have a many-to-many relationship via two navigation properties Person.Departments and Department.Persons:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Department> Departments { get; set; }
}

public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Person> Persons { get; set; }
}

The relationship is explicitely configured the following:

public class PersonsContext : AuditDbContext
{
    // ...
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().HasMany<Department>(x => x.Departments).WithMany(x => x.Persons);
    }
}

However, omitting the fluent configuration doesn’t change the behavior. But deriving from DbContext instead of AuditDbContext fixes the problem.

Here is the part of the main method with the issue:

// ...
context.Departments.Add(new Department() { Id = 1, Name = "Development" });
context.Departments.Add(new Department() { Id = 2, Name = "Research" });

context.SaveChanges();

context.Persons.Add(new Person() { Id = 1, Name = "Alice", Departments = context.Departments.ToList() });
context.Persons.Add(new Person() { Id = 2, Name = "Bob", Departments = context.Departments.ToList() });

// ArgumentNullException will be thrown here: Value cannot be null. (Parameter 'name')
context.SaveChanges();

Expected behavior No exception should be thrown and the audit information should be written.

Libraries (specify the Audit.NET extensions being used including version):

  • Audit.EntityFramework.Core: 16.4.0

Target .NET framework:

  • .NET 5.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
thepirat000commented, Jan 21, 2021

First of all I want to thank you for your cooperation sharing a project that reproduces the issue, I wish all the issues were described as you did…

The problem was because of EF Core changes on the latest version that, as usually, affects the libraries that depends on the representation of the EF ChangeTracker.

Anyway I’ve fixed the issue on version 16.4.1, please upgrade your references and re-test.

1reaction
pergerchcommented, Jan 22, 2021

Thank you for your quick response. No worries about the demo project, it should anyway be the standard in my opinion.

Thanks a lot. The new version works like a charm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Value cannot be null. (Parameter 'key')" using API method ...
I found that my many to many relationships between (product and predefinedElements), (markets and predefinedElements) and (actuation type and ...
Read more >
Field-only navigation properties in many ...
System.ArgumentNullException: Value cannot be null. (Parameter 'member') at System.Linq.Expressions.Expression.
Read more >
Value cannot be null. Parameter name: persistentType
I created an OpenAccess Fluent project in Visual Studio 2010. I added a few models and some mappings. This project built fine in...
Read more >
Value cannot be null. Parameter name: _unity_self ...
The script is not mine, but is a basicFPCC script by AlucardJay. Anyways, the full error is below: ArgumentNullException: Value cannot be null....
Read more >
Value cannot be null. Parameter name: source-Entity Framework
I got this error when I had an invalid Type for an entity property. public Type ObjectType {get;set;}. When I removed the property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found