The inherited properties are not saved in EF Core
Closed this issue · 2 comments
mytrash commented
Hello,
I created the example project. When I make new migration: dotnet ef migration new Initialize
- The migration creates new Question table with 3 columns Id, Title, Description. Columns InsertedAt, UpdatedAt are not inclued into the migration.
- When I add missing columns into the database manually...
The EF saves only 3 columns (Id, Title, Description) into the database. Columns **InsertedAt, UpdatedAt are not saved. Triggers Inserting and Updating are called correctly. - The EF loads only 3 columns (Id, Title, Description) from the database. Columns InsertedAt, UpdatedAt are not loaded.
Could you help me where the problem is?
Thank you Myth.
public abstract class Trackable
{
public DateTime InsertedAt { get; private set; }
public DateTime UpdatedAt { get; private set; }
static Trackable()
{
Triggers<Trackable>.Inserting += (entry) => { entry.Entity.InsertedAt = entry.Entity.UpdatedAt = DateTime.UtcNow; };
Triggers<Trackable>.Updating += (entry) => { entry.Entity.UpdatedAt = DateTime.UtcNow; };
}
}
public class Question : Trackable
{
public long Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
public class ApplicationDbContext : DbContextWithTriggers
{
public DbSet<Question> Questions { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
NickStrupat commented
I'm not an expert on EF migrations, but those properties both have private setters. Did you try removing private
and check if the migration works?
NickStrupat commented
Any update? Did that work?