Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create a view with navigation property #27854

Closed
fstojanac opened this issue Apr 21, 2022 · 2 comments
Closed

Can't create a view with navigation property #27854

fstojanac opened this issue Apr 21, 2022 · 2 comments
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@fstojanac
Copy link

fstojanac commented Apr 21, 2022

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

@AndriySvyryd AndriySvyryd self-assigned this Apr 21, 2022
@AndriySvyryd AndriySvyryd added this to the 6.0.x milestone Apr 21, 2022
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 21, 2022
@AndriySvyryd AndriySvyryd removed their assignment Apr 21, 2022
@fstojanac
Copy link
Author

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

@AndriySvyryd
Copy link
Member

May/June

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants