I want to change column type of property using fluent api, but i have an error
The expression 'x => x.NestedProp.Prop1' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'.
please, i dont want to use DataAnnotations
Here is my code (Classes):
public class Class1 {
public int Id { get; set; }
public NestedProp NestedProp { get; set; } = new NestedProp();
}
public class NestedProp {
public decimal Prop1 { get; set; }
}
And OnModelCreating:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Class1>(e =>
{
e.OwnsOne(t => t.NestedProp);
e.Property(p => p.NestedProp.Prop1).HasColumnType("NUMERIC(38, 16)");
});
}
Please give the following a try and pay attention to the
Id
property in your classNestedProp
. EF (Core) needs in every model a primary key, otherwise it will not work.Models
OnModelCreating
Here you will find some more explanation. You have to define the configuration for each model.
I recommend to use
IEntityTypeConfiguration<T>
instead of configure everything inOnModelCreating
.A nice introduction for using the Fluent API in EF Core would you find here or here.
EDIT:
The Solution above will create two tables, because it implements two own data types. Not the in question asked complex type in on column. Therefore I will give advise for this solution too. This owned type mapping is realizable via
entity.OwnsOne(...)
method. It is also possible to split in like mentioned in this MS doc article. In this article you will find how to configure it explicitly too.Here the example with your code and fluent API:
Models
OnModelCreating