Search code examples
performanceentity-framework-coreef-code-first

Is it better for performance not to add fk to tables in the code first approach entity framework core


there is a topic I was wondering friends. I'm running EntityFrameworkCore code first, and in one article I came across an expression: "If you need virtual jobs, see Include ()." Now, if I remove all the foreign keys in the tables, is the performance better or does it have an effect.

Thanks


Solution

  • Ef core will generate FKs whether you include them or not in your model.

    e.g.

    example source : https://learn.microsoft.com/en-us/ef/core/modeling/shadow-properties

    class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }
    
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    
        public List<Post> Posts { get; set; }
    }
    
    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
    
        public Blog Blog { get; set; }
    }
    

    "For example, the following code listing will result in a BlogId shadow property being introduced to the Post entity."

    So in conclusion removing them will have no effect in the performance of your queries