✅ 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'.
74 Replies
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.
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 thoucant I just not povide a ID as my database will just make those?
no, because these wont be provided by the DB
these will be part of your migrations
ah
how would EF be able to detect a change to them if their IDs are unknown/can change?
oké
so, if I would like to add my series to the database on database creation, how do I need to do that?
"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
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
we use a pre-startup service at work for stuff like this
that syncs up entities from the codebase to the database
so kind of like a query you run after creating the database?
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
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)
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
, etcI was thinking of letting the DB handle it as its already a temporal table and the DateUpdated will mainly be used by powerBI
wouldn't that only work for dbfirst thou?
and probably not all data will go through my api
thats what I also had struggles with as im doing code first now😅
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
it would be nicer to not have to do that though as I rather have everything being created by code
ah actually there is something mm
ValueGeneratedOnAddOrUpdate()
which apparently you can override
do you have a link to the documentation?
Generated Values - EF Core
How to configure value generation for properties when using Entity Framework Core
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
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
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
unless i have messed something up, it also doesnt work
at least, when I eddit something in my database using mssms it doesnt update the UpdatedAt field
the temporal table works which is nice
I will give it a go here and see but im using postgresql
hopefully wont make much of a difference
well u would be able to see it in the migrations or so I think
I will try dropping my db and remaking it, maybe that does something different
cool
that doesnt work then
and for some reason it also needs me to povide an ID
DateTime?
because it can be empty at first
ofc
Updated at still stays null though
looks like it works
this is the generated migration
how does your code look inside your DB context
for a start I just tried this
For SQL Server thou u would use getutcdate() apparently, for postgresql current_timestamp
They both still dont work
Generated Values - EF Core
How to configure value generation for properties when using Entity Framework Core
dammit sql
getutcdate() needed to be getdate()
now I wonder if there is one that is just OnUpdate
otherwise there isnt much meaning to make DateTime nullable
There is
You already get this info with ValidFrom
Now to fix my GUIDs not working because of some stupid reason
True, but unfortionately non technical people aslo need to understand my code 😑
ah there is perfect
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
depends
if there is a way to change the time from the gatdate() to amsterdam then I need it
Yes, it's possible
The datetime generated when you configure ValidFrom is utc
you can convert from utc to whatever
3
I use nodatime so conversion is pretty straight forward
are you converting in api or in the database
why would you convert in the database?
database should always be utc
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
but surely hes fetching from the database? u can convert upon fetching
thats why I would like the updated at instead of the validFrom ValidTo even though it does the same thing
thats also an option
or do you mean hes looking directly in the database?
it uses a direct query
cool, updatedAt is in CET now
nvmnd generateValueOnUpdate() doesnt work
yeah u need to use a trigger
I saw
can I make the trigger in Ef Core?
u can add it to your migration yes
but it would be a manual addition
dammit
then They will just have to use the temporal thing I guess
thanks for the help everyone
as an example of what mine would look like
at this point I dont think the ValueGeneratedOnUpdate is even needed thou as the trigger will act on its own according.
Thnx, I'll look into it again on monday probably
First going to enjoy my weekend😁
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
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