Reputation: 148
I created a new Web API project (AOT), after I created the basic tables and the basic settings, I needed to create my first migration Add-Migration InitialCreate;
but I get an exception:
Unable to create a 'DbContext' of type ''. The exception 'Model building is not supported when publishing with NativeAOT. Use a compiled model.' was thrown while attempting to create an instance for the different patterns supported at design time.
My ApplicationDbContext
code:
public class ApplicationDbContext: DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Department>? Departments { get; set; }
public DbSet<Unit>? Units { get; set; }
}
My Program.cs
is:
var builder = WebApplication.CreateSlimBuilder(args);
var ConnectionString = "Connection";
var connectionString = builder.Configuration.GetConnectionString(ConnectionString);
builder.Services.AddDbContext<ApplicationDbContext>(x =>
{
x.UseSqlServer(connectionString);
});
// Add services to the container.
builder.Services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("CorsPolicy");
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Provider and version information:
Microsoft.EntityFrameworkCore.SqlServer
Upvotes: 0
Views: 2168
Reputation: 2041
For EF 8.0 and 9.0 you need to set <PublishAot>false</PublishAot>
while running any of the tools.
Upvotes: 0