Reputation: 1281
I used data annotations validation before, Now I want to change Fluent Validation. Because Now We change the project to clean architecture.
When I send the data from postman, The request goes to exception.
System.InvalidOperationException: The property 'X.Overview' is of type 'XOverview' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
when using dataannotations validation
Domain X.cs
public class X
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public XOverview Overview { get; set; }
}
public class XOverview
{
[Required]
public int Id { get; set; }
[Required]
public string Content { get; set; }
}
in DbConext.cs
public DbSet<XOverview> XOverview { get; set; }
// removed the rest
The above is working fine.
Now I want to change to fluent validation.
when using fluent validation
Domain X.cs
public class X
{
public XOverview Overview { get; set; }
}
public class XOverview
{
public int Id { get; set; }
public string Content { get; set; }
}
// Fluent validation for X
public class XConfiguration : IEntityTypeConfiguration<x>
{
public void Configure(EntityTypeBuilder<x> builder)
{
builder.HasKey(i => i.Id);
builder.Property(i =>i.Overview)
.IsRequired();
}
}
public class XOverviewConfiguration : IEntityTypeConfiguration<XOverview>
{
public void Configure(EntityTypeBuilder<XOverview> builder)
{
builder.Property(i =>i.Id)
.IsRequired();
builder.Property(i =>i.Content)
.IsRequired();
}
}
What is the mistake I did when change to fluent validation ? Please help to find the issue.
Upvotes: 1
Views: 2181
Reputation: 205719
First of all, this is not "fluent validation", but EF Core entity model fluent configuration.
Second, the concrete problem is this line
builder.Property(i =>i.Overview)
It's because by EF Core terminology, Overview
is not a property, but navigation property (or simply navigation), hence is not allowed in methods called Property(...)
. Instead, it can be used only in relationship configuring methods (Has{One|Many}(...)
/ With({One|Many}(...)
) or the EF Core 5.0 introduced Navigation(...)
.
So basically what you need is to replace Property
with Navigation
, e.g.
builder.Navigation(i => i.Overview)
.IsRequired();
Upvotes: 3