-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
How to disable any conventions related to relationship discovery? #30906
Comments
@Abdragiz This is probably not going to be easy, and likely to be fragile. Can you provide more details on why you want to, "create two migrations for this model: one with basic CREATE TABLE sql statements and another with foreign keys and constrains according to model relationships?" |
I'm currently just testing things, but I need to populate a database with data. According to https://www.postgresql.org/docs/current/populate.html it's better to load data into a database that doesn't have indexes and foreign key constraints. So I wanted to populate database like this: IMigrator migrator = dbContext.GetInfrastructure().GetRequiredService<IMigrator>();
await migrator.MigrateAsync("InitialCreate").ConfigureAwait(false); // apply a migration that only has CREATE TABLE statements
ImportDataAsync(dbContext);
dbContext.Database.MigrateAsync() // apply migrations that add indexes, foreign key constraints, etc. |
By the way, I found out about RelationshipDiscoveryConvention |
About 300 GB of xml files. |
It's true that when bulk-loading massive amounts of data, it's generally recommended to remove any constraints/indexes and re-add them after the procedure, since checking/updating them during the import takes more time. IIRC the same is probably true with SQL Server as well. But in general, rather than trying to tweak the conventions to remove the relationships, I'd recommend generating a SQL script instead and doing the tweaking there... The bulk import process is likely to be single-time only, so there's not much value in getting EF to generate exactly the migration you want (without foreign keys) just to add them right back in the next migration... |
Ok, i'll look into it. |
If I use multiple DbContexts for my database I have to manually ignore all the relations with entities not included in smaller contexts? No workaround to prevent exceptions like "System.InvalidOperationException: Unable to determine the relationship represented by navigation ..." ? |
Repost from https://stackoverflow.com/q/76263986/16478093
I have model with one-to-one and one-to-many relationships similar to this:
I want to create two migrations for this model: one with basic CREATE TABLE sql statements and another with foreign keys and constrains according to model relationships, but added relationship navigations are automatically discovered and included in initial migration.
I tried two ways:
Disable relationship discovery by
[NotMapped]
attribute or byEntityTypeBuilder<TEntity>.Ignore()
method. I think it will work, but I need to find ALL relationship navigations in my code, add[NotMapped]
attribute, create initial migration, remove ALL added attributes and then create relationships migration.Disable relationship discovery convention by removing
RelationshipDiscoveryConvention
inConfigureConventions
method.But after disabling
RelationshipDiscoveryConvention
I got new error, similar to this:Unable to determine the relationship represented by navigation 'Blog.Posts' of type 'ICollection<Post>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
So, removing
RelationshipDiscoveryConvention
does not actually disable relationship discovery but disables configuring class properties as relationship navigations.What convention I need to remove to disable relationship discovery?
The text was updated successfully, but these errors were encountered: