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

Copies of tables - without navigation? #2924

Closed
patrykdawid opened this issue Nov 30, 2020 · 1 comment · Fixed by #4269
Closed

Copies of tables - without navigation? #2924

patrykdawid opened this issue Nov 30, 2020 · 1 comment · Fixed by #4269

Comments

@patrykdawid
Copy link

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

@ajcvickers ajcvickers transferred this issue from dotnet/efcore Dec 1, 2020
@ajcvickers ajcvickers added this to the Backlog milestone Dec 1, 2020
@ajcvickers
Copy link
Member

@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
      );

@ajcvickers ajcvickers self-assigned this Feb 23, 2023
@ajcvickers ajcvickers modified the milestones: Backlog, 8.0.0 Feb 23, 2023
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 19, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 28, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 30, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants