Skip to content

Support complex properties on owned types #33170

Description

@lzoller-flexibilIT

File a bug

I'm trying to set up a database containing different tables that have to be implemented as ComplexType or Owned respectively. I'm using attributes to configure that.

The ef command line tool succussfully creates a migration for the model but the generated code contains errors for every occurence of a ComplexType within an Owned type because the class OwnedNavigationBuilder doesn't contain a method for ComplexType.

I can't make everything Owned because of missing relations and I can't make everything ComplexType because of a 1:n relation from another table.

As the ef tool successfully generates the migration code and accepts the model this must be a bug in EFcore.
The migration code generated keeps the project from building so e.g. ef migrations remove will fail, the migration classes have to be removed manually.

Include your code

Here the relevant snippets from the model.

Starting point is the Invoice class containing a list of LineItems:

    public class Invoice
    {
        public Company customer { get; set; }
        public Project project { get; set; }
        ...
        public List<LineItem> line_items { get; set; }
    }

The LineItem is Owned and contains a Proposition:

    [Owned]
    public class LineItem
    {
        public string? LineItemType { get; set; }
        public string? Name { get; set; }
        public string? Description { get; set; }
        ...
        public Proposition Proposition { get; set; }
    }

Finally the Proposition is a ComplexType:

    [ComplexType]
    public class Proposition
    {
        public string? Type { get; set; }
        public int? Id { get; set; }
        public string? Name { get; set; }
        ...
    }

Creating a migration based on this model via...

dotnet ef migrations add Initial

...is successful:

Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'

Include stack traces

(no execution possible)

Include verbose output

The migration classes produced looks like this (the complier error occurs at b1.ComplexProperty<>):

            ...
            modelBuilder.Entity("MyProject.Model.Invoice", b =>
                {
                    ...
                    b.HasOne("PapierkramShared.Model.Company", "customer")
                        .WithMany()
                        .HasForeignKey("customerId")
                        .OnDelete(DeleteBehavior.Cascade)
                        .IsRequired();

                    b.HasOne("PapierkramShared.Model.Project", "project")
                        .WithMany()
                        .HasForeignKey("projectId")
                        .OnDelete(DeleteBehavior.Cascade)
                        .IsRequired();

                    b.OwnsMany("MyProject.Model.LineItem", "line_items", b1 =>
                        {
                            ...
                            b1.Property<string>("LineItemType")
                                .HasColumnType("nvarchar(max)")
                            b1.Property<string>("Name")
                                .HasColumnType("nvarchar(max)")
                            b1.Property<string>("Description")
                                .HasColumnType("nvarchar(max)")
                            ...
                            b1.ComplexProperty<Dictionary<string, object>>("Proposition", "MyProject.Model.InvoiceDetails.line_items#LineItem.Proposition#Proposition", b2 =>
                                {
                                    b2.IsRequired();
                                    b2.Property<string>("Type")
                                        .IsRequired()
                                        .HasColumnType("nvarchar(max)")
                                    b2.Property<int>("Id")
                                        .HasColumnType("int")
                                    b2.Property<string>("Name")
                                        .IsRequired()
                                        .HasColumnType("nvarchar(max)")
                                    ...

The compiler error produces is this one (translated from German):
error CS1061: "OwnedNavigationBuilder" does not contain a definition for "ComplexProperty" and no accessible extension method "ComplexProperty" accepting a first argument of type "OwnedNavigationBuilder" could be found.

Include provider and version information

EF Core version: 8.0.2
EF Core Command-line Tools: 8.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer / also tried: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: NET 8.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.9.0

Activity

  1. JoasE commented on Feb 27, 2024

    @JoasE
    Contributor

    I was about to create this issue myself! However, I was going to make it a feature request as I was using the fluent api to configure my entity relations and noticed the ComplexProperty builder method missing on the OwnedNavigationBuilder.

    Should I create a separate issue for this? As the fact that the migrations generator is generating code that has a compile time error is an issue of itself, but it could be fixed by implementing functionality.

    My code:

    public class Entity
    {
        public int Id { get; set; }
    
        public OwnedEntity OwnedEntity { get; set; }
    
        public ComplexType ComplexType { get; set; }
    }
    
    public class OwnedEntity
    {
        public int Id { get; set; }
    
        public ComplexType ComplexType { get; set; }
    }
    
    public class ComplexType
    {
        public string Name { get; set; }
    
        public string Description { get; set; }
    }
    
    class TestDbContext : DbContext
    {
        public TestDbContext() : base(new DbContextOptionsBuilder<TestDbContext>().UseSqlServer().Options)
        {
        }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<Entity>(cfg =>
            {
                cfg.ComplexProperty(x => x.ComplexType);
    
                cfg.OwnsOne(x => x.OwnedEntity, cfg =>
                {
                    // Method to configure ComplexProperty does not exist!
                    cfg.ComplexProperty(x => x.ComplexType); // Error CS1061  'OwnedNavigationBuilder<Entity, OwnedEntity>' does not contain a definition for 'ComplexProperty' and no accessible extension method 'ComplexProperty' accepting a first argument of type 'OwnedNavigationBuilder<Entity, OwnedEntity>' could be found
                });
            });
        }
    }
  2. lzoller-flexibilIT commented on Feb 27, 2024

    @lzoller-flexibilIT
    Author

    Hi @JoasE, that's curious :-)
    Creating your own issue sounds good.
    Thanks for hint with the fluent api and the sample code - even though I was trying to prevent using it...

  3. ajcvickers commented on Feb 28, 2024

    @ajcvickers
    Contributor

    @AndriySvyryd Do you know if we are already tracking this?

  4. AndriySvyryd commented on Feb 28, 2024

    @AndriySvyryd
    Member

    It looks like I missed creating an issue for this

  5. changed the title [-]unable to use a ComplexType in an Owned type[/-] [+]Support complex properties on Owned types[/+] on Feb 28, 2024
  6. added this to the Backlog milestone on Mar 7, 2024
  7. added theissue type on Jun 15, 2025
  8. NoahS2003 commented on Dec 2, 2025

    @NoahS2003

    At the very least, before support is implemented, there should at least be an error on migration creation (possibly linking to this issue).

    Until this is fixed, this issue should be listed as a bug - auto-generated migrations should not contain compilation errors - it makes it annoying to revert, as removing a migration from the command line requires the code to build properly.

  9. changed the title [-]Support complex properties on Owned types[/-] [+]Support complex properties on owned types[/+] on Dec 21, 2025
  10. IS4Code commented on Apr 21, 2026

    @IS4Code

    Would be cool to see some improvements here.

    It should also be noted that it is also possible to get a complex type into an owned entity in ConfigureConventions via configurationBuilder.ComplexProperties.

  11. NoahS2003 commented on Apr 21, 2026

    @NoahS2003

    @IS4Code - are you saying that that's a workaround for this issue? Or that it's another way to cause the issue?

  12. IS4Code commented on Apr 21, 2026

    @IS4Code

    I can't explore it further with my current setup, though I think it likely causes the same issues.

  13. Taha-Babapour commented on Oct 20, 2026

    @Taha-Babapour

    two years later and nothing ? guys that's really a needed feature I have a 2 layered value object that I can't map correctly because of this, you said use Complex instead of owned, what if I want it to carry Identity ?!

  14. added 3 commits that reference this issue on Oct 28, 2026
    6e24b75
    539564c
    67c2ae1
  15. added a commit that references this issue on Nov 21, 2026
    177ef9d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions