✅ 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
restingphantomOP8mo 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
Pobiega8mo 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
restingphantomOP8mo ago
cant I just not povide a ID as my database will just make those?
Pobiega
Pobiega8mo ago
no, because these wont be provided by the DB these will be part of your migrations
restingphantom
restingphantomOP8mo ago
ah
Pobiega
Pobiega8mo ago
how would EF be able to detect a change to them if their IDs are unknown/can change?
restingphantom
restingphantomOP8mo 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
Pobiega8mo 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
restingphantomOP8mo 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
Pobiega8mo ago
we use a pre-startup service at work for stuff like this that syncs up entities from the codebase to the database
restingphantom
restingphantomOP8mo ago
so kind of like a query you run after creating the database?
Pobiega
Pobiega8mo 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
restingphantomOP8mo 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
Pobiega8mo 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
restingphantomOP8mo 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
leowest8mo ago
wouldn't that only work for dbfirst thou?
restingphantom
restingphantomOP8mo 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
leowest8mo 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
restingphantomOP8mo ago
it would be nicer to not have to do that though as I rather have everything being created by code
leowest
leowest8mo ago
ah actually there is something mm ValueGeneratedOnAddOrUpdate() which apparently you can override
restingphantom
restingphantomOP8mo ago
do you have a link to the documentation?
leowest
leowest8mo 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
restingphantomOP8mo 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
leowest8mo 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
restingphantomOP8mo ago
unless i have messed something up, it also doesnt work
restingphantom
restingphantomOP8mo ago
No description
restingphantom
restingphantomOP8mo ago
No description
restingphantom
restingphantomOP8mo 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
leowest8mo ago
I will give it a go here and see but im using postgresql
restingphantom
restingphantomOP8mo ago
hopefully wont make much of a difference
leowest
leowest8mo ago
well u would be able to see it in the migrations or so I think
restingphantom
restingphantomOP8mo ago
I will try dropping my db and remaking it, maybe that does something different
restingphantom
restingphantomOP8mo ago
cool
No description
restingphantom
restingphantomOP8mo ago
that doesnt work then and for some reason it also needs me to povide an ID
leowest
leowest8mo ago
DateTime? because it can be empty at first
restingphantom
restingphantomOP8mo ago
ofc Updated at still stays null though
leowest
leowest8mo ago
looks like it works
No description
leowest
leowest8mo ago
No description
leowest
leowest8mo ago
this is the generated migration
restingphantom
restingphantomOP8mo ago
how does your code look inside your DB context
leowest
leowest8mo 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
restingphantomOP8mo 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
restingphantomOP8mo ago
Generated Values - EF Core
How to configure value generation for properties when using Entity Framework Core
restingphantom
restingphantomOP8mo ago
dammit sql getutcdate() needed to be getdate()
leowest
leowest8mo ago
now I wonder if there is one that is just OnUpdate otherwise there isnt much meaning to make DateTime nullable
restingphantom
restingphantomOP8mo ago
There is
linqisnice
linqisnice8mo ago
You already get this info with ValidFrom
restingphantom
restingphantomOP8mo 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
leowest8mo ago
ah there is perfect
linqisnice
linqisnice8mo 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
restingphantomOP8mo ago
depends if there is a way to change the time from the gatdate() to amsterdam then I need it
linqisnice
linqisnice8mo ago
Yes, it's possible The datetime generated when you configure ValidFrom is utc you can convert from utc to whatever
linqisnice
linqisnice8mo ago
3
No description
linqisnice
linqisnice8mo ago
I use nodatime so conversion is pretty straight forward
restingphantom
restingphantomOP8mo ago
are you converting in api or in the database
linqisnice
linqisnice8mo ago
why would you convert in the database?
leowest
leowest8mo ago
database should always be utc
restingphantom
restingphantomOP8mo 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
linqisnice8mo ago
but surely hes fetching from the database? u can convert upon fetching
restingphantom
restingphantomOP8mo 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
linqisnice8mo ago
or do you mean hes looking directly in the database?
restingphantom
restingphantomOP8mo ago
it uses a direct query cool, updatedAt is in CET now nvmnd generateValueOnUpdate() doesnt work
leowest
leowest8mo ago
yeah u need to use a trigger
restingphantom
restingphantomOP8mo ago
I saw can I make the trigger in Ef Core?
leowest
leowest8mo ago
u can add it to your migration yes but it would be a manual addition
restingphantom
restingphantomOP8mo ago
dammit then They will just have to use the temporal thing I guess thanks for the help everyone
leowest
leowest8mo ago
as an example of what mine would look like
No description
leowest
leowest8mo ago
No description
leowest
leowest8mo ago
at this point I dont think the ValueGeneratedOnUpdate is even needed thou as the trigger will act on its own according.
leowest
leowest8mo ago
No description
restingphantom
restingphantomOP8mo ago
Thnx, I'll look into it again on monday probably First going to enjoy my weekend😁
leowest
leowest8mo 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
restingphantomOP8mo 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
Want results from more Discord servers?
Add your server