dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.63k stars 3.15k forks source link

Can't create a view with navigation property #27854

Closed fstojanac closed 2 years ago

fstojanac commented 2 years ago

Since the upgrade to the EF Core 6, I can no longer create new views if they include navigation properties to entities. This is something that worked in EF Core 5, so I believe this is a regression.

Steps to reproduce

Entity

using System;
using System.Collections.Generic;

namespace Test.Data;

public class TestEntity
{
    public Guid Id { get; set; }

    public ICollection<TestView> TestViews { get; set; }
}

View

using System;

namespace Test.Data;

public class TestView
{
    public Guid TestId { get; set; }
    public string Property { get; set; }

    public TestEntity TestEntity { get; set; }
}

Entity configuration

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Test.Data;

public class TestEntityMap : IEntityTypeConfiguration<TestEntity>
{
    public virtual void Configure(EntityTypeBuilder<TestEntity> builder)
    {
        builder.ToTable("test");

        builder.HasKey(x => x.Id);

        builder.Property(e => e.Id).IsRequired();
    }
}

View configuration

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Test.Data;

public class TestViewMap : IEntityTypeConfiguration<TestView>
{
    public virtual void Configure(EntityTypeBuilder<TestView> builder)
    {
        builder.ToView("test_view");

        builder.HasKey(x => new { x.TestId, x.Property });

        builder.Property(e => e.TestId).IsRequired();
        builder.Property(e => e.Property).IsRequired();

        builder.HasOne(x => x.TestEntity).WithMany(x => x.TestViews).HasForeignKey(x => x.TestId);
    }
}

When I try to add a new migration (dotnet ef migrations add test) I get the following exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'name')
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
   at Microsoft.EntityFrameworkCore.Metadata.StoreObjectIdentifier.Table(String name, String schema)
   at Microsoft.EntityFrameworkCore.RelationalKeyExtensions.GetName(IReadOnlyKey key)
   at EFCore.NamingConventions.Internal.NameRewritingConvention.ProcessKeyAdded(IConventionKeyBuilder keyBuilder, IConventionContext`1 context)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnKeyAdded(IConventionKeyBuilder keyBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnKeyAddedNode.Run(ConventionDispatcher dispatcher)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.DelayedConventionScope.Run(ConventionDispatcher dispatcher)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Run()
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Dispose()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 properties, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 clrMembers, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasKey(Expression`1 keyExpression)
   at Test.Data.TestViewMap.Configure(EntityTypeBuilder`1 builder) in [redacted]/Test.Data/TestViewMap.cs:line 12
   at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration`1 configuration)

Version information

EF Core version: 6.0.4 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET 6.0.4 Operating system: macOS 12.2.1 IDE: Rider

fstojanac commented 2 years ago

@AndriySvyryd awesome, thanks! Do you have a rough idea when this might be released?

AndriySvyryd commented 2 years ago

May/June