Skip to content
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

Closed
Abdragiz opened this issue May 16, 2023 · 8 comments
Closed

How to disable any conventions related to relationship discovery? #30906

Abdragiz opened this issue May 16, 2023 · 8 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@Abdragiz
Copy link

Repost from https://stackoverflow.com/q/76263986/16478093
I have model with one-to-one and one-to-many relationships similar to this:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Author Author { get; set; } // one-to-one reference navigation
    public ICollection<Post> Posts { get; } // one-to-many collection navigation
}
public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
}

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:

  1. Disable relationship discovery by [NotMapped] attribute or by EntityTypeBuilder<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.

  2. Disable relationship discovery convention by removing RelationshipDiscoveryConvention in ConfigureConventions method.

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
    ArgumentNullException.ThrowIfNull(configurationBuilder);
    configurationBuilder.Conventions.Remove(typeof(RelationshipDiscoveryConvention));
}

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?

@Abdragiz Abdragiz changed the title How disable any conventions related to relationship discovery? How to disable any conventions related to relationship discovery? May 16, 2023
@ajcvickers
Copy link
Member

@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?"

@Abdragiz
Copy link
Author

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.

@ajcvickers
Copy link
Member

@Abdragiz How much data do you have?

@roji Any thoughts on this from a PostgreSQL perspective?

@Abdragiz
Copy link
Author

By the way, I found out about RelationshipDiscoveryConvention
from here #27647 (comment).

@Abdragiz
Copy link
Author

@ajcvickers

How much data do you have?

About 300 GB of xml files.

@roji
Copy link
Member

roji commented May 16, 2023

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...

@Abdragiz
Copy link
Author

Ok, i'll look into it.

@great-fed
Copy link

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 ..." ?

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label May 26, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

4 participants