Skip to content

Copies of tables - without navigation? #2924

Closed

Description

@patrykdawid

Friends!
I need to turn off the navigation, but the rest of the action is to remain the same, e.g. a column containing a foreign key must be created. I want to achieve similar behavior as Envers for nHibernate (https://github.com/nhibernate/nhibernate-envers) - data copy per table but without navigation.

Is it possible? It's great if you could show an example in the code...

EF Core version:
Database provider: Pomelo.EntityFrameworkCore.MySql
Target framework: .NET Core 3.1 or .NET 5.0

Activity

  1. transferred this issue fromdotnet/efcoreon Dec 1, 2020
  2. added this to the Backlog milestone on Dec 1, 2020
  3. ajcvickers commented on Dec 2, 2020

    @ajcvickers
    Contributor

    @patrykdawid Here's an example of configuring a relationship without navigations. We will add this to the docs.

    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class Post
    {
        public int Id { get; set; }
        public string Content { get; set; }
    
        public int? BlogId { get; set; }
    }
    
    public class SomeDbContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseSqlServer(Your.ConnectionString);
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>(e => e.HasMany<Post>().WithOne());
        }
    }

    This maps to the following tables in SQL Server:

          CREATE TABLE [Blogs] (
              [Id] int NOT NULL IDENTITY,
              [Name] nvarchar(max) NULL,
              CONSTRAINT [PK_Blogs] PRIMARY KEY ([Id])
          );
    
          CREATE TABLE [Posts] (
              [Id] int NOT NULL IDENTITY,
              [Content] nvarchar(max) NULL,
              [BlogId] int NULL,
              CONSTRAINT [PK_Posts] PRIMARY KEY ([Id]),
              CONSTRAINT [FK_Posts_Blogs_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [Blogs] ([Id]) ON DELETE NO ACTION
          );
  4. self-assigned this
    on Feb 23, 2023
  5. modified the milestones: Backlog, 8.0.0 on Feb 23, 2023
  6. added 8 commits that reference this issue on Feb 25, 2023
    e20432f
    671a66c
    94ff962
    f33e5ed
    c7b38aa
    93b4c5e
    1ba858c
    bb39d2d
  7. added a commit that references this issue on Mar 19, 2023
    9a1e6f2
  8. added 2 commits that reference this issue on Mar 28, 2023
    f5d7299
    dd7c736
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions