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

Mix of owned and normal entity types in hierarchy should cause validation error #10200

Closed
SharpSeeEr opened this issue Oct 31, 2017 · 2 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@SharpSeeEr
Copy link

Observed Behavior

Using the following setup with an Address class that is used as an Owned Entity and as a base class for the ContactAddress class:

public class OwnedTypesContext : DbContext
{
    public DbSet<House> Houses { get; set; }
    public DbSet<Contact> Contacts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=OwnedTypesInheritence;Trusted_Connection=True;");
        base.OnConfiguring(optionsBuilder);
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<House>()
            .OwnsOne(e => e.Address);
        
        base.OnModelCreating(modelBuilder);
    }
}

public class Address
{
    [MaxLength(500)] public string StreetAddress1 { get; set; }
    [MaxLength(200)] public string City { get; set; }
    [MaxLength(2)]   public string State { get; set; }
    [MaxLength(5)]   public string Zip { get; set; }
}

public class ContactAddress : Address
{
    public int Id { get; set; }
    public int ContactId { get; set; }
    public Contact Contact { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    [MaxLength(100)] public string FirstName { get; set; }
    [MaxLength(200)] public string LastName { get; set; }
    public List<ContactAddress> Addresses { get; set; } = new List<ContactAddress>();
}

public class House
{
    public int Id { get; set; }
    public Address Address { get; set; }
    public string OwnerName { get; set; }
}

Creating the initial migration works as expected - the ContactAddress table does not have a Discriminator column but the ContextModelSnapshot does show Discriminator column:

modelBuilder.Entity("EFCoreOwnedTypeInheritence.ContactAddress", b =>
    {
        b.Property<int>("Id")
            .ValueGeneratedOnAdd();

        b.Property<string>("City")
            .HasMaxLength(200);

        b.Property<int>("ContactId");

        b.Property<string>("Discriminator") // <---  Exists in Model Snapshot
            .IsRequired();

        b.Property<string>("Label")
            .HasMaxLength(25);

        b.Property<string>("State")
            .HasMaxLength(2);

        b.Property<string>("StreetAddress1")
            .HasMaxLength(500);

        b.Property<string>("Zip")
            .HasMaxLength(5);

        b.HasKey("Id");

        b.HasIndex("ContactId");

        b.ToTable("ContactAddress");

        b.HasDiscriminator<string>("Discriminator").HasValue("ContactAddress");
    });

All subsequent migrations, even if no changes were made, result in the following:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "Discriminator",
        table: "ContactAddress");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "Discriminator",
        table: "ContactAddress",
        nullable: false,
        defaultValue: "");
}

If you run dotnet ef database update it gives the error:

ALTER TABLE DROP COLUMN failed because column 'Discriminator' does not exist in table 'ContactAddress'

Expected Behavior

The ContextModelSnapshot would not have any reference to a Discriminator column for an Owned Type, and migrations would not continuously DropColumn(name: "Discriminator") and AddColumn(name: "Discriminator")

Steps to reproduce

  1. Clone Test Case Project
  2. Run the following commands:
dotnet restore
dotnet ef migrations add Initial
dotnet database update
dotnet ef migrations add Second
dotnet database update
  1. View the .\Migrations*_Second.cs migration file.

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.0.0
Operating system: Microsoft Windows 10 Pro Version 1709 OS Build 16299.19
IDE: Visual Studio 2017 15.4.1, VS Code 1.17.2

@ajcvickers
Copy link
Member

Related to #9630 and discussion in #9536

@ajcvickers
Copy link
Member

@SharpSeeEr To be honest, we didn't consider the case of having a base type which is an owned entity and then another mapping for the derived type that is not an owned entity. Right now this should be considered not supported--we will use this bug to track adding better validation for this when building a model. Beyond this, issue #9630 is tracking support for inheritance in owned type, which will ultimately decide whether or not this mix of owned/normal entities in a hierarchy will be supported.

@ajcvickers ajcvickers changed the title Non-existent Discriminator column on Entity that inherits from an OwnedEntity Mix of owned and normal entity types in hierarchy should cause validation error Nov 17, 2017
@ajcvickers ajcvickers added this to the 2.1.0 milestone Nov 17, 2017
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
AndriySvyryd added a commit that referenced this issue Feb 1, 2018
Validate owned type inheritance
Allow to disconnect mapped base type

Fixes #10200
Fixes #10715
AndriySvyryd added a commit that referenced this issue Feb 1, 2018
Validate owned type inheritance
Allow to disconnect mapped base type

Fixes #10200
Fixes #10715
AndriySvyryd added a commit that referenced this issue Feb 2, 2018
Validate owned type inheritance
Allow to disconnect mapped base type

Fixes #10200
Fixes #10715
@AndriySvyryd AndriySvyryd removed their assignment Feb 2, 2018
@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 Feb 2, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants