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

Improve exception message for missing dependent in table splitting #14456

Closed
IndigoHealth opened this issue Jan 18, 2019 · 5 comments
Closed
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-3.0 type-bug
Milestone

Comments

@IndigoHealth
Copy link

IndigoHealth commented Jan 18, 2019

Weird problem referencing owned types from classes that inherit from other classes. I created a simple repro. Please see https://stackoverflow.com/q/54246707/1637105

@ajcvickers
Copy link
Member

ajcvickers commented Jan 23, 2019

Note for triage: simple repro:

public class IdentityUser
{
    public string Id { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public PersonalName Name { get; set; }
    public ValidationToken ValidationToken { get; set; }
}

[Owned]
public class PersonalName
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

[Owned]
public class ValidationToken
{
    public int ValidationCode { get; set; }
    public DateTime ExperiationDateUTC { get; set; }
}

public class BloggingContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
    }

    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<ApplicationUser> AppUsers { get; set; }
}

public class Program
{
    public static void Main()
    {
        using (var context = new BloggingContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
        }
    }
}

Stack trace:

Unhandled Exception: System.InvalidOperationException: Cannot use table 'IdentityUser' for entity type 'ValidationToken' since it is being used for entity type 'PersonalName' and there is no relationship between their primary keys.
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateSharedTableCompatibility(IReadOnlyList`1 mappedTypes, String tableName)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateSharedTableCompatibility(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ValidatingConvention.Apply(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureDeleted()
   at Program.Main() in C:\Stuff\TwoTwoPreview\TwoTwoPreview\Program.cs:line 82

@IndigoHealth
Copy link
Author

Note the workaround in the answer to the original SO post. The problem only happens when the relationship is configured via attribute markup.

@IndigoHealth
Copy link
Author

I thought that the workaround was to configure the owned relations in OnModelCreating, but I've added (and configured) a bunch of "owned" types to one of my inherited classes and now I get a new variant on the error:

[InvalidOperationException] The entity of type 'Patient' is sharing the table 'AspNetUsers' with entities of type 'EmergencyContact', but there is no entity of this type with the same key value that has been marked as 'Added'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.

Note that Patient derives from AspNetUsers, and EmergencyContact is a POCO class that doesn't contain an Id property.

@IndigoHealth
Copy link
Author

Never mind... This is the error you get when you neglect to initialize the property that is a reference to the owned entity prior to adding the entity to the DB.

The error message is flat-out wrong. The error message should tell me that Patient.EmergencyContact can not be null. Or, if that level of detail isn't available, it should at least give me a clue that I need to initialize "owned" entity properties.

@AndriySvyryd AndriySvyryd modified the milestones: Backlog, 5.0.0 Dec 11, 2019
@AndriySvyryd AndriySvyryd changed the title add-migration problems with owned type Improve exception message for missing dependent in table splitting Jul 17, 2020
@AndriySvyryd
Copy link
Member

Fixed in 6351aa1

@AndriySvyryd AndriySvyryd modified the milestones: 5.0.0, 3.0.0 Aug 27, 2020
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 20, 2020
@AndriySvyryd AndriySvyryd removed their assignment Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-3.0 type-bug
Projects
None yet
Development

No branches or pull requests

3 participants