NullReferenceException in EF 5-6.1.1 with two navigation properties to the same type

c# code-first entity-framework entity-framework-5 entity-framework-6

Question

I'd like to start with that I have a workaround for this issue - but I spent a few hours today figuring out the cause of the exception, so I'd thought I'd share

Given two entities in the domain:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Ticket
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual User Owner { get; set; }

    public int? LockedByUserId { get; set; }
    public virtual User LockedByUser { get; set; }

    [Timestamp]
    public byte[] ETag { get; set; }
}

The following configuration:

public class TicketConfiguration : EntityTypeConfiguration<Ticket>
{
    public TicketConfiguration()
    {
        HasRequired(x => x.Owner);

        HasOptional(x => x.LockedByUser)
            .WithMany()
            .HasForeignKey(x => x.LockedByUserId);

        Property(x => x.ETag)
            .IsConcurrencyToken(true);
    }
}

And this seed:

protected override void Seed(DataContext context)
    {
        var users = context.Set<User>();
        var user = new User
        {
            Name = "Foo"
        };
        users.AddOrUpdate(x => x.Name, user);
        user = users.SingleOrDefault(x => x.Name == "Foo") ?? user;

        var tickets = context.Set<Ticket>();
        tickets.AddOrUpdate(x=>x.Name, new Ticket
        {
            Name = "Bar",
            Owner = user,
        });
    }

I get an exception with this:

static void Main()
    {
        var config = new Migrations.Configuration { CommandTimeout = 3600 };
        var migrator = new DbMigrator(config);
        migrator.Update();

        using (var transaction = GetTransaction()) // I've tried with and without transaction
        {
            var context = new DataContext();
            var userId = context.Set<User>().Where(x=>x.Name == "Foo").Select(x=>x.Id).Single();
            var ticket = context.Set<Ticket>().Single(x=>x.Name == "Bar");
            ticket.LockedByUserId = userId;

            context.SaveChanges(); 
            // Exception thrown here 'System.NullReferenceException' 
            //at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.GetOtherEndOfRelationship(IEntityWrapper wrappedEntity)
            //at System.Data.Entity.Core.Objects.EntityEntry.AddRelationshipDetectedByForeignKey(Dictionary`2 relationships, Dictionary`2 principalRelationships, EntityKey relatedKey, EntityEntry relatedEntry, RelatedEnd relatedEndFrom)
            //at System.Data.Entity.Core.Objects.EntityEntry.DetectChangesInForeignKeys()
            //at System.Data.Entity.Core.Objects.ObjectStateManager.DetectChangesInForeignKeys(IList`1 entries)
            //at System.Data.Entity.Core.Objects.ObjectStateManager.DetectChanges()
            //at System.Data.Entity.Core.Objects.ObjectContext.DetectChanges()
            //at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
            //at System.Data.Entity.Internal.InternalContext.GetStateEntries(Func`2 predicate)
            //at System.Data.Entity.Internal.InternalContext.GetStateEntries()
            //at System.Data.Entity.Infrastructure.DbChangeTracker.Entries()
            //at System.Data.Entity.DbContext.GetValidationErrors()
            //at System.Data.Entity.Internal.InternalContext.SaveChanges()
            //at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
            //at System.Data.Entity.DbContext.SaveChanges()
            //at EntityFrameworkFkNull.Program.Main(String[] args) in h:\Projects\Spikes\EntityFrameworkFkNull\EntityFrameworkFkNull\Program.cs:line 27
            //at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
            //at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
            //at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
            //at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
            //at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
            //at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
            //at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
            //at System.Threading.ThreadHelper.ThreadStart()
            transaction.Complete();
        }
    }

Get the full solution to try yourself here: https://github.com/mvidacovich/EntityFrameworkFkNull

I believe this is because Ticket has two different foreign keys to User but only one of them is explicitly configured.

This affects EF 5 to Ef 6 as far as I've tested myself.

So, that begs the question: Is it expected that EF throws an exception there?

1
4
7/29/2014 7:00:02 PM

Popular Answer

The workaround is to have an "OwnerId" property on Ticket. (See fix branch in the solution in github)

So, Ticket becomes:

public class Ticket
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int? OwnerId { get; set; }
    public virtual User Owner { get; set; }

    public int? LockedByUserId { get; set; }
    public virtual User LockedByUser { get; set; }

    [Timestamp]
    public byte[] ETag { get; set; }
}

And Ticket's configuration changes to:

public class TicketConfiguration : EntityTypeConfiguration<Ticket>
{
    public TicketConfiguration()
    {
        HasRequired(x => x.Owner)
            .WithMany()
            .HasForeignKey(x=>x.OwnerId);

        Property(x => x.OwnerId)
            .HasColumnName("Owner_Id");

        HasOptional(x => x.LockedByUser)
            .WithMany()
            .HasForeignKey(x => x.LockedByUserId);

        Property(x => x.ETag)
            .IsConcurrencyToken(true);
    }
}

Notice the explicit OwnerId now. See this for the full (fixed) solution: https://github.com/mvidacovich/EntityFrameworkFkNull/tree/Fix

3
7/29/2014 7:00:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow