efcore: DataAnnotations not working in UWP at design-time

I created the following minimal example, a new project in VS2015.3 on win10 with added dependencies and the App.Config like described in the tutorial. I called the project SQLMinimalTest. The following code is the only code I added to a fresh UWP template.

using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Windows.UI.Xaml.Media;

namespace SQLMinimalTest
{
public class Blog
{
    [Key]
    public int BlogID { get; set; }
    public string RandomString { get; set; }

    [NotMapped]
    public SolidColorBrush Brush
    {
        get { return _Brush; }
        set { _Brush = value; }
    }
    [NotMapped]
    private SolidColorBrush _Brush;
}

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=BloggingTest.db");
    }
}
}

When I now type Add-Migration mig1 -Verbose in Package Manager Console, I get the following Error:

PM> Add-Migration mig1 -Verbose
Using startup project 'SQLMinimalTest'.
Using project 'SQLMinimalTest'
Build started...
Build succeeded.
Using application base 'D:\Programmieren\C#\VS2015 Testprojects\SQLMinimalTest\SQLMinimalTest\bin\x86\Debug\'.
Using application configuration 'D:\Programmieren\C#\VS2015 Testprojects\SQLMinimalTest\SQLMinimalTest\App.Config'
Using current directory 'D:\Programmieren\C#\VS2015 Testprojects\SQLMinimalTest\SQLMinimalTest\bin\x86\Debug\'.
Finding DbContext classes...
Using context 'BloggingContext'.
System.InvalidOperationException: The key {'TempId'} contains properties in shadow state and is referenced by a relationship from 'SolidColorBrush' to 'Blog.Brush'. Configure a non-shadow principal key for this relationship.
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.KeyConvention.Apply(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.Validate()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Validate()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass14_0.<GetModel>b__0(Object k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_4(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.<>c__DisplayClass7_0.<ConfigureContextServices>b__9(IServiceProvider _)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The key {'TempId'} contains properties in shadow state and is referenced by a relationship from 'SolidColorBrush' to 'Blog.Brush'. Configure a non-shadow principal key for this relationship.

and the project.json:

{
  "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.0"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

This is not the expected outcome. With the previous version I did not have this issue.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 31 (19 by maintainers)

Most upvoted comments

@ole1986 - In the default ApplicationUser the PK is of string type so when creating the relationship targeting ApplicationUser as above, EF needs the foreign key property to be of type string. The ForeignKeyAttribute tells EF to use UserID as FK property. Now if UserID is of string type all works fine as you showed in your work-around. But as per the first model UserID is of type Guid. Since user specified it using annotation, EF will use it as foreign key property. But Guid type FK property needs to target Guid Principal key. Since PK of ApplicationUser is different type, EF will create shadow principal key temporarily while building model so that user can replace it using fluent API. If that is not done the exception as above is thrown during validation.