New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fluent API ValueGeneratedOnAddOrUpdate() does not seem to be working. #3955
Comments
@Eneuman In addition to telling EF when they are generated, you need to setup some mechanism to actually generate the values. You can use For the updated one, you probably want to add a trigger. There is no first class API to do this, but you can use Alternatively, if you want to generate them on the app server (rather than in the database), you can override SaveChanges and just set the values there. public override int SaveChanges()
{
ChangeTracker.DetectChanges();
var now = DateTime.UtcNow;
foreach (var item in ChangeTracker.Entries<Blog>().Where(e => e.State == EntityState.Added))
{
item.Property("Created").CurrentValue = now;
item.Property("Updated").CurrentValue = now;
}
foreach (var item in ChangeTracker.Entries<Blog>().Where(e => e.State == EntityState.Modified))
{
item.Property("Updated").CurrentValue = now;
}
return base.SaveChanges();
} |
@rowanmiller Thanks for the quick reply! And the docs says: "Value generated on add or update" Maybe add a section in the docs that describes how you supply your own value generator? |
It would be great if this was possible:
|
I agree that something is wrong here. The documentation at http://ef.readthedocs.org/en/latest/modeling/generated-properties.html#id7 strongly suggests that a value will be generated, and uses the specific case of Modified and Created DateTime values. |
Cool, I will update the docs to make it clear that you are telling EF that a value will be generated. There are some cases (integers keys, GUID keys, byte[] concurrency tokens) where EF will setup a way to generate values... and for anything else you need to setup some form of generation. |
Update to docs out for review dotnet/EntityFramework.Docs#88 |
Closing as this is not being tracked by the PR in the docs repo |
They're calling each other @rowanmiller - overriding Why are all overloads of SaveChanges[Async] virtual? Shouldn't only the most relevant version be virtual to avoid this kind of duplication/confusion? Would it be possible to even provide one code path for both sync and async code paths? |
@Eneuman @rowanmiller I have been working on an EF Core version of my EntityFramework.Triggers library at https://github.com/NickStrupat/EntityFramework.Triggers |
I'm trying to use Without
|
I'm trying to add a "Created" and "Updated" column to one of my entities.
I want "Created" to contain the DateTime when the entity was first saved in the databse and "Updated" the last time is was changed.
Using the Fluent API code example, from the "Generated properties" doc page, produces a "Cannot insert NULL into column Created" error.
This is my setup:
Entity:
public DateTime Created{ get; set; }
public DateTime Updated { get; set; }
Fluent API
b.Property(u => u.Uppdaterades).ValueGeneratedOnAddOrUpdate();
b.Property(u => u.Skapades).ValueGeneratedOnAdd();
Am I missing something?
The text was updated successfully, but these errors were encountered: