dotnet / dotnet/efcore

Improve navigations on complex type exception message

Open
#33,525 1 comment 0 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-complex-types area-model-building customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

This fails model validation:

```
Unhandled exception. System.InvalidOperationException: Complex type 'Blog.ComplexContainer#ComplexContainer (ComplexContainer)' has no properties defines. Configure at least one property or don't include this type in the model.
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.g__Validate|7_0(IConventionTypeBase typeBase, <>c__DisplayClass7_0&) in /Users/roji/projects/efcore/src/EFCore/Infrastructure/ModelValidator.cs:line 187
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidatePropertyMapping(IModel model, IDiagnosticsLogger`1 logger) in /Users/roji/projects/efcore/src/EFCore/Infrastructure/ModelValidator.cs:line 140
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger) in /Users/roji/projects/efcore/src/EFCore/Infrastructure/ModelValidator.cs:line 48
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger) in /Users/roji/projects/efcore/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:line 52
```

We should also fix the typo `has no properties defines` -> `has no properties defined`. Once the model validation is fixed, we should also test that saving and querying works.

Repro

```c#
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

var container = new ComplexContainer
{
Containee1 = new(),
Containee2 = new()
};

_ = await context.Blogs.Where(b => b.ComplexContainer == container).ToListAsync();

public class BlogContext : DbContext
{
public DbSet Blogs { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().ComplexProperty(b => b.ComplexContainer);
}
}

public class Blog
{
public int Id { get; set; }
public ComplexContainer ComplexContainer { get; set; }
}

public class ComplexContainer
{
public ComplexContainee1 Containee1 { get; set; }
public ComplexContainee2 Containee2 { get; set; }
}

public class ComplexContainee1
{
public int Id { get; set; }
}

public class ComplexContainee2
{
public int Id { get; set; }
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.