C
C#10mo ago
Joellao

EF Migrations 101

Hello everyone. I'm starting a new project and wanted to know why the hell are the migrations so hard to work with. I have the Database entities and a working state of the current implementation. If I need to create a new entity with some relationship it fucks up everything. The new migration is not updating the db, or it updates the DB but then it complains when inserting data that foreign key constraint is going nuts. Do you have a preferred way on how to work with this, because deleting all the migration folder and creating a new migration each time I need to add some relationship, doesn't seem the way to deal with this. Do you guys also set the relationships in the OnModelCreating or you just let ef handle that? Question and ranting at the same time, sorry for that
12 Replies
dreadfullydistinct
What exactly are you doing at the moment, because it doesn’t sound right Typically the workflow is you create your initial migration, apply that to the database using the command line, and then when you want to make changes you generate a new smaller migration, then apply that as wel I usually let ef handle the relationships, it’s pretty good at inferring them these days Also, not sure how we can help with ‘foreign key constraint is going nuts’ or ‘fucks up everything’ - would be nice to see some examples and errors
Pobiega
Pobiega10mo ago
Migrations shouldn't be a pain in the ass, except data migrations - or when you are moving between branches and adding migrations in each branch (that is a pain).
Angius
Angius10mo ago
Seems like you're doing something wrong, because migrations Just Work™
Pobiega
Pobiega10mo ago
Trivial relationships I let EF handle, non trivial are configured in IEntityTypeConfiguration<T> classes (which is just a split way of doing it in OnModelCreating
Joellao
JoellaoOP10mo ago
All my problems started with a ManyToMany relationship. User and Groups. I have UserEntityu and GroupsEntity. I also created a middle Entity to map this called UserGroupEntity. That seems quite simple. The first problem with this arose with having in the UserEntity a CreatedGroups and then a ParticipatingGroups collections. I wanted also to avoid having the middle table relationship in the two entities. Finally I made it work with this piece of horror
modelBuilder
.Entity<GroupEntity>()
.HasOne(p => p.Creator)
.WithMany(u => u.Groups)
.HasForeignKey(p => p.CreatorId);

modelBuilder
.Entity<GroupEntity>()
.HasMany(e => e.Members)
.WithMany(e => e.Participant)
.UsingEntity<UserGroupEntity>(
j => j
.HasOne(pt => pt.User)
.WithMany(t => t.ParticipatingGroups)
.HasForeignKey(pt => pt.UserId),
j => j
.HasOne(pt => pt.GroupEntity)
.WithMany(p => p.Memberships)
.HasForeignKey(pt => pt.GroupId));
modelBuilder
.Entity<GroupEntity>()
.HasOne(p => p.Creator)
.WithMany(u => u.Groups)
.HasForeignKey(p => p.CreatorId);

modelBuilder
.Entity<GroupEntity>()
.HasMany(e => e.Members)
.WithMany(e => e.Participant)
.UsingEntity<UserGroupEntity>(
j => j
.HasOne(pt => pt.User)
.WithMany(t => t.ParticipatingGroups)
.HasForeignKey(pt => pt.UserId),
j => j
.HasOne(pt => pt.GroupEntity)
.WithMany(p => p.Memberships)
.HasForeignKey(pt => pt.GroupId));
Today I wanted to create a Friendship relationship, so a List of UserEntity in the UserEntity should make the trick. Nope, it doesn't work because the UserId is already in a relationship and can only be once (or something similar was the error, don't have it under my hands right now). So back to manually do the relationship by hand. Everything compiled, the database updated, but now there is a new column in the Users table that is not needed, and of course I don't have that mapping anywhere in the code and that's driving me crazy Now I reverted everything back, in the meantime have lost several migrations, maybe like 5, because of these problems. But whatever, it's a new project so whatever if I don't have the full history of migrations But still I'm getting some passive trauma I guess :kekw:
dreadfullydistinct
Wouldn;t you probably want a middle entity for the many to many friendship relation too?
dreadfullydistinct
Extra column could be https://learn.microsoft.com/en-us/ef/core/modeling/shadow-properties which EF makes for relationships sometimes
Shadow and Indexer Properties - EF Core
Configuring shadow and indexer properties in an Entity Framework Core model
dreadfullydistinct
Also, you can generally avoid having to explicitly configure stuff if you are clever about naming your properties, for instance it should be able to work out that Creator has a foreign key of CreatorId
Joellao
JoellaoOP10mo ago
Yeah I also used the middle entity for the friendship also, but when doing that it created the new Column called UserEntityId inside the Users table. It expects to be filled somehow but I don't have that relationship. I'm also using the Creator and CreatorId thingy explicitly
dreadfullydistinct
You used the same middle entity? Or a different one?
Joellao
JoellaoOP10mo ago
UserEntity is the users one. FriendshipEntity for the middle Don't have it anymore since I reverted back the branch But qutie easy FriendshipSender with FriendshipSenderId FriendshipReceiver and that was it And in the UserEntity I had the Friends collection with UserEntity as a type and FriendRelationships with the middleclass Or something similar to this one The naming I mean
dreadfullydistinct
hmm, not super familiar with many to many in EF core as I've only done it a few times 😕. What did your FriendRelationships middle class look like?
Want results from more Discord servers?
Add your server