✅ Seed cannot be added because of required property ID

The seed entity for entity type 'Series' cannot be added because no value was provided for the required property 'Id'.
No description
No description
74 Replies
restingphantom
restingphantom3mo ago
trying to fill in the id like this ↓ , gives me the error : The seed entity for entity type 'Series' cannot be added because another seed entity with the same key value for {'Id'} has already been added.
No description
Pobiega
Pobiega3mo ago
you can't do it like that. each time the migration runs, it will have a different ID and conflict with HasData, your IDs must be static the alternative is to run your own seeding function as part of your app, instead of as part of the migrations. that has its own downsides thou
restingphantom
restingphantom3mo ago
cant I just not povide a ID as my database will just make those?
Pobiega
Pobiega3mo ago
no, because these wont be provided by the DB these will be part of your migrations
restingphantom
restingphantom3mo ago
ah
Pobiega
Pobiega3mo ago
how would EF be able to detect a change to them if their IDs are unknown/can change?
restingphantom
restingphantom3mo ago
oké so, if I would like to add my series to the database on database creation, how do I need to do that?
Pobiega
Pobiega3mo ago
"on database creation"? that would be via the migration then and that means you need to have hardcoded IDs or do it as part of a manually written migration step, but that means EF wont know about it and you wont get any help with update statements to it
restingphantom
restingphantom3mo ago
hmmm For my DB I need this set of series so that I can later link them to my investments (like an Enum), what would be best, setting al the GUIDs manually at database creation or setting all the series after database creation using some sort of Init query or something
Pobiega
Pobiega3mo ago
we use a pre-startup service at work for stuff like this that syncs up entities from the codebase to the database
restingphantom
restingphantom3mo ago
so kind of like a query you run after creating the database?
Pobiega
Pobiega3mo ago
sort of, in a way but fully automated and keeps the entites in sync, so it detects if a new one is added or changed
restingphantom
restingphantom3mo ago
Cool Ill leave this for now I think, do you maybe also know something about how to set up a DateUpdated column and where to implement the date setting (database or API)
Pobiega
Pobiega3mo ago
some database support automatic updating of a timestamp whenever the record is modified I prefer doing it in code thou, since I might have several different things to track CreatedAt/By, ModifiedAt/By, etc
restingphantom
restingphantom3mo ago
I was thinking of letting the DB handle it as its already a temporal table and the DateUpdated will mainly be used by powerBI
leowest
leowest3mo ago
wouldn't that only work for dbfirst thou?
restingphantom
restingphantom3mo ago
and probably not all data will go through my api thats what I also had struggles with as im doing code first now😅
leowest
leowest3mo ago
because on code first u only have hasdefaultvalue that doesn't do updates but when u create directly in the db u do have attributes u can add to the table creation that will make it auto update a column with say current utc time etc and those are performed by the db
restingphantom
restingphantom3mo ago
it would be nicer to not have to do that though as I rather have everything being created by code
leowest
leowest3mo ago
ah actually there is something mm ValueGeneratedOnAddOrUpdate() which apparently you can override
restingphantom
restingphantom3mo ago
do you have a link to the documentation?
leowest
leowest3mo ago
so for datetime u need to create a trigger its a bit more complext then just doing it via code imo but its a one time setup
restingphantom
restingphantom3mo ago
I think that this is better because now the value can be changed without the API and not breaking anything in the process and I can just run a migration on a different server to create the database automaticly
leowest
leowest3mo ago
ngl im reading it and im a bit lost myself on how its implemented haha, I would have to give it a go and see. its dependent on the engine u use sql server for example have something called isrowvesion
restingphantom
restingphantom3mo ago
unless i have messed something up, it also doesnt work
restingphantom
restingphantom3mo ago
No description
restingphantom
restingphantom3mo ago
No description
restingphantom
restingphantom3mo ago
at least, when I eddit something in my database using mssms it doesnt update the UpdatedAt field the temporal table works which is nice
leowest
leowest3mo ago
I will give it a go here and see but im using postgresql
restingphantom
restingphantom3mo ago
hopefully wont make much of a difference
leowest
leowest3mo ago
well u would be able to see it in the migrations or so I think
restingphantom
restingphantom3mo ago
I will try dropping my db and remaking it, maybe that does something different
restingphantom
restingphantom3mo ago
cool
No description
restingphantom
restingphantom3mo ago
that doesnt work then and for some reason it also needs me to povide an ID
leowest
leowest3mo ago
DateTime? because it can be empty at first
restingphantom
restingphantom3mo ago
ofc Updated at still stays null though
leowest
leowest3mo ago
looks like it works
No description
leowest
leowest3mo ago
No description
leowest
leowest3mo ago
this is the generated migration
restingphantom
restingphantom3mo ago
how does your code look inside your DB context
leowest
leowest3mo ago
for a start I just tried this
public class BlogConfiguration : IEntityTypeConfiguration<Blog>
{
public void Configure(EntityTypeBuilder<Blog> builder)
{
builder.Property(x => x.CreatedAt)
.HasDefaultValueSql("current_timestamp");

builder.Property(x => x.UpdatedAt)
.HasDefaultValueSql("current_timestamp")
.ValueGeneratedOnAddOrUpdate();
//.Metadata.SetAfterSaveBehavior(Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Save);
}
}
public class BlogConfiguration : IEntityTypeConfiguration<Blog>
{
public void Configure(EntityTypeBuilder<Blog> builder)
{
builder.Property(x => x.CreatedAt)
.HasDefaultValueSql("current_timestamp");

builder.Property(x => x.UpdatedAt)
.HasDefaultValueSql("current_timestamp")
.ValueGeneratedOnAddOrUpdate();
//.Metadata.SetAfterSaveBehavior(Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Save);
}
}
For SQL Server thou u would use getutcdate() apparently, for postgresql current_timestamp
restingphantom
restingphantom3mo ago
They both still dont work
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure a many-to-many relationship between Ventures and Sdgs.
modelBuilder.Entity<Venture>()
.HasMany(e => e.Sdgs)
.WithMany(e => e.Ventures);

modelBuilder.Entity<Venture>().Property(x => x.CreatedAt)
.HasDefaultValueSql("getutcdate()");

modelBuilder.Entity<Venture>().Property(x => x.UpdatedAt)
.HasDefaultValueSql("getutcdate()")
.ValueGeneratedOnAddOrUpdate();

// Configure the Venture entity to use a temporal table, enabling tracking of data changes over time.
modelBuilder.Entity<Venture>().ToTable("Venture", e => e.IsTemporal());
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure a many-to-many relationship between Ventures and Sdgs.
modelBuilder.Entity<Venture>()
.HasMany(e => e.Sdgs)
.WithMany(e => e.Ventures);

modelBuilder.Entity<Venture>().Property(x => x.CreatedAt)
.HasDefaultValueSql("getutcdate()");

modelBuilder.Entity<Venture>().Property(x => x.UpdatedAt)
.HasDefaultValueSql("getutcdate()")
.ValueGeneratedOnAddOrUpdate();

// Configure the Venture entity to use a temporal table, enabling tracking of data changes over time.
modelBuilder.Entity<Venture>().ToTable("Venture", e => e.IsTemporal());
}
restingphantom
restingphantom3mo ago
Generated Values - EF Core
How to configure value generation for properties when using Entity Framework Core
restingphantom
restingphantom3mo ago
dammit sql getutcdate() needed to be getdate()
leowest
leowest3mo ago
now I wonder if there is one that is just OnUpdate otherwise there isnt much meaning to make DateTime nullable
restingphantom
restingphantom3mo ago
There is
linqisnice
linqisnice3mo ago
You already get this info with ValidFrom
restingphantom
restingphantom3mo ago
Now to fix my GUIDs not working because of some stupid reason True, but unfortionately non technical people aslo need to understand my code 😑
leowest
leowest3mo ago
ah there is perfect
linqisnice
linqisnice3mo ago
Alright, but you don't need a separate UpdatedAt property you're already versioning with temporal tables which has ValidFrom and ValidTo, which detail when updates occured
restingphantom
restingphantom3mo ago
depends if there is a way to change the time from the gatdate() to amsterdam then I need it
linqisnice
linqisnice3mo ago
Yes, it's possible The datetime generated when you configure ValidFrom is utc you can convert from utc to whatever
linqisnice
linqisnice3mo ago
3
No description
linqisnice
linqisnice3mo ago
I use nodatime so conversion is pretty straight forward
restingphantom
restingphantom3mo ago
are you converting in api or in the database
linqisnice
linqisnice3mo ago
why would you convert in the database?
leowest
leowest3mo ago
database should always be utc
restingphantom
restingphantom3mo ago
because my internship location uses powerbi which connects directly to the db thats why i want to do as much as possible there to make it easier for them
linqisnice
linqisnice3mo ago
but surely hes fetching from the database? u can convert upon fetching
restingphantom
restingphantom3mo ago
thats why I would like the updated at instead of the validFrom ValidTo even though it does the same thing thats also an option
linqisnice
linqisnice3mo ago
or do you mean hes looking directly in the database?
restingphantom
restingphantom3mo ago
it uses a direct query cool, updatedAt is in CET now nvmnd generateValueOnUpdate() doesnt work
leowest
leowest3mo ago
yeah u need to use a trigger
restingphantom
restingphantom3mo ago
I saw can I make the trigger in Ef Core?
leowest
leowest3mo ago
u can add it to your migration yes but it would be a manual addition
restingphantom
restingphantom3mo ago
dammit then They will just have to use the temporal thing I guess thanks for the help everyone
leowest
leowest3mo ago
as an example of what mine would look like
No description
leowest
leowest3mo ago
No description
leowest
leowest3mo ago
at this point I dont think the ValueGeneratedOnUpdate is even needed thou as the trigger will act on its own according.
leowest
leowest3mo ago
No description
restingphantom
restingphantom3mo ago
Thnx, I'll look into it again on monday probably First going to enjoy my weekend😁
leowest
leowest3mo ago
yeah im not sure why u gave up on that, its manual yes, but u have to do it once to the migration. anyway hf
restingphantom
restingphantom3mo ago
Didnt really give up on it per se. I'll probably get back to it on monday when I'm back in the mood to work on it as some other thing didnt work as well