efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Model Snapshot won't compile with nested types

Open cpa-level-it opened this issue 3 years ago • 2 comments

File a bug

I tried making a simple nested type project to debug the problem I have in the following issue https://github.com/dotnet/efcore/issues/31726 where I'm unable to create a migration for a nested type.

I was finally able to create a migration by using the fluent api instead of only relying on attributes.

The problem is that using the following code correctly create a migration, but then the Model Snapshot won't compile.

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     modelBuilder
         .Entity<UserContext>()
         .ComplexProperty(
         x => x.CompanyIdentificationData,
         b => b.ComplexProperty(x => x.Belgium));
 }

Here is a snipped of the snapshot, I added comments where there is an error.

        protected override void BuildModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("ProductVersion", "8.0.0-rc.1.23419.6")
                .HasAnnotation("Proxies:ChangeTracking", false)
                .HasAnnotation("Proxies:CheckEquality", false)
                .HasAnnotation("Proxies:LazyLoading", true)
                .HasAnnotation("Relational:MaxIdentifierLength", 128);

            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

            modelBuilder.Entity("EfCore8Tests.Models.UserContext", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");

                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

                    b.ComplexProperty<Dictionary<string, object>>("CompanyIdentificationData", "EfCore8Tests.Models.UserContext.CompanyIdentificationData#CompanyIdentificationData", b1 =>
                        {
// INVALID CODE
                            .IsRequired()
                            b1.Property<bool>("Val")
                                .HasColumnType("bit");

                            b1.ComplexProperty<Dictionary<string, object>>("Belgium", "EfCore8Tests.Models.UserContext.CompanyIdentificationData#CompanyIdentificationData.Belgium#BelgiumCompanyIdentificationData", b2 =>
                                {
// INVALID CODE
                                    .IsRequired()
                                    b2.Property<string>("EntityBceNumber")
                                        .IsRequired()
                                        .HasColumnType("nvarchar(max)");

                                    b2.Property<bool>("HasVATNumber")
                                        .HasColumnType("bit");

                                    b2.Property<string>("SeatBceNumber")
                                        .IsRequired()
                                        .HasColumnType("nvarchar(max)");
                                });
                        });

                    b.HasKey("Id");

                    b.ToTable("UserContexts");
                });
#pragma warning restore 612, 618
        }

Here is a link to a repository reproducing this error https://github.com/cpa-level-it/EfCoreComplextTypeProblem

The problematic migration with the invalid model snapshot are already created.

Include verbose output

Please include --verbose output when filing bugs about the dotnet ef or Package Manager Console tools.

Use triple-tick fences for tool output. For example:

PM> dotnet ef dbcontext list --verbose --project .\EfCore8Tests
Using project 'C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj'.
Using startup project 'C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj'.
Writing 'C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\obj\EfCore8Tests.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\cpa\AppData\Local\Temp\tmplhqiqr.tmp /verbosity:quiet /nologo C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj
Writing 'C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\obj\EfCore8Tests.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\cpa\AppData\Local\Temp\tmpe4axqw.tmp /verbosity:quiet /nologo C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj
Build started...
dotnet build C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj /verbosity:quiet /nologo /p:PublishAot=false
C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\Models\BelgiumCompanyIdentificationData.cs(8,23): warning CS8618: Non-nullable property 'SeatBceNumber' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj]
C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\Models\BelgiumCompanyIdentificationData.cs(9,23): warning CS8618: Non-nullable property 'EntityBceNumber' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj]

Build succeeded.

C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\Models\BelgiumCompanyIdentificationData.cs(8,23): warning CS8618: Non-nullable property 'SeatBceNumber' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj]
C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\Models\BelgiumCompanyIdentificationData.cs(9,23): warning CS8618: Non-nullable property 'EntityBceNumber' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj]
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.49
Build succeeded.
dotnet exec --depsfile C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\bin\Debug\net8.0\EfCore8Tests.deps.json --additionalprobingpath d:\nuget-cache --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\bin\Debug\net8.0\EfCore8Tests.runtimeconfig.json C:\Users\cpa\.dotnet\tools\.store\dotnet-ef\8.0.0-rc.1.23419.6\dotnet-ef\8.0.0-rc.1.23419.6\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll dbcontext list --assembly C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\bin\Debug\net8.0\EfCore8Tests.dll --project C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj --startup-assembly C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\bin\Debug\net8.0\EfCore8Tests.dll --startup-project C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\EfCore8Tests.csproj --project-dir C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\ --root-namespace EfCore8Tests --language C# --framework net8.0 --nullable --working-dir C:\Users\cpa\source\repos\EfCore8Tests --verbose
Using assembly 'EfCore8Tests'.
Using startup assembly 'EfCore8Tests'.
Using application base 'C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\bin\Debug\net8.0'.
Using working directory 'C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests'.
Using root namespace 'EfCore8Tests'.
Using project directory 'C:\Users\cpa\source\repos\EfCore8Tests\EfCore8Tests\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'EfCore8Tests'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'ComplexTypeDbContext'.
EfCore8Tests.Models.ComplexTypeDbContext

Include provider and version information

EF Core version: 8.0.0-rc.1.23419.6 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: NET 8 RC 1 Operating system: Windows 11 22H2 IDE: Visual Studio 2022 17.8.0 Preview 1.0

cpa-level-it avatar Sep 14 '23 07:09 cpa-level-it

@cpa-level-it Duplicate of https://github.com/dotnet/efcore/issues/31665. Please try with the latest daily build.

ajcvickers avatar Sep 14 '23 08:09 ajcvickers

This work perfectly with the daily build, thanks.

cpa-level-it avatar Sep 14 '23 09:09 cpa-level-it