EF 5-6.1.1中的NullReferenceException,有两个指向相同类型的导航属性

本文关键字:两个 属性 导航 同类型 NullReferenceException 中的 EF | 更新日期: 2023-09-27 18:15:34

我想从我有一个解决这个问题的方法开始-但是我今天花了几个小时弄清楚异常的原因,所以我想我应该分享

给定域中的两个实体:

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; }
}

下面的配置:

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);
    }
}

和这个种子:

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,
        });
    }

我得到一个异常:

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();
        }
    }

在这里获得完整的解决方案:https://github.com/mvidacovich/EntityFrameworkFkNull

我相信这是因为Ticket对User有两个不同的外键,但只有一个是显式配置的。

根据我自己的测试,这会影响EF 5到EF 6。

那么,这就引出了一个问题:是否期望EF在那里抛出异常?

EF 5-6.1.1中的NullReferenceException,有两个指向相同类型的导航属性

解决方法是在Ticket上有一个"OwnerId"属性。(参见github解决方案中的fix分支)

所以,Ticket变成:

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; }
}

和Ticket的配置更改为:

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);
    }
}

现在注意显式的OwnerId。完整的(固定的)解决方案参见此:https://github.com/mvidacovich/EntityFrameworkFkNull/tree/Fix