Support complex properties on owned types
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
## 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 **LineItem**s:
```C#
public class Invoice
{
public Company customer { get; set; }
public Project project { get; set; }
...
public List line_items { get; set; }
}
```
The **LineItem** is **Owned** and contains a **Proposition**:
```C#
[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**:
```C#
[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("LineItemType")
.HasColumnType("nvarchar(max)")
b1.Property("Name")
.HasColumnType("nvarchar(max)")
b1.Property("Description")
.HasColumnType("nvarchar(max)")
...
b1.ComplexProperty>("Proposition", "MyProject.Model.InvoiceDetails.line_items#LineItem.Proposition#Proposition", b2 =>
{
b2.IsRequired();
b2.Property("Type")
.IsRequired()
.HasColumnType("nvarchar(max)")
b2.Property("Id")
.HasColumnType("int")
b2.Property("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
Contributor guide
Assessment
This issue has not been assessed yet.