-
Notifications
You must be signed in to change notification settings - Fork 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
Copies of tables - without navigation? #2924
Comments
@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
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: