The property 'Tripstop.Birthdate' could not be mapped because it is of type 'DateOnly'
Closed this issue · 2 comments
Hopefully not a newbie question however getting this error when attempting to generate a migration for the new DateOnly field I have added. This is dotnet core web api project, using sqlserver for the database. (or is saving DateOnly to SQLServer a different part of the puzzle I have to solve, with your DateOnlyTimeOnly nuget package handing getting it working through the Web API interface?)
Error:
$dotnet ef migrations add dateonlyfield
Build started...
Build succeeded.
System.InvalidOperationException: The property 'Tripstop.Birthdate' could not be mapped because it is of type 'DateOnly', which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Added in Program.cs:
.
.
.
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services
.AddControllers(options => options.UseDateOnlyTimeOnlyStringConverters())
.AddJsonOptions(options => options.UseDateOnlyTimeOnlyStringConverters());
var app = builder.Build();
.
.
.
Then added to my model class:
public DateOnly Birthdate { get; set; }
sorry - I see the database/EF side seems to be handled by a nuget package that someone created with converters:
Yes, this package only affects ASP.NET side of things. Hm, I was under the impression, that EF Core already works with DateOnly out of the box in .NET 6.
Will, glad you've found the solution.