Database First EF Core and Inverse Property

Database First EF Core and Inverse Property

I have a database where many of the tables have a set of audit data rows:

I have a database where many of the tables have a set of audit data rows:

public Guid CreatedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
public Guid? ModifiedByUserId { get; set; }
public DateTime? ModifiedDate { get; set; }

For example, the Area and Citation table both have such a set of rows. The userIds are linked by a foreign key to the User table (as you would expect.)

When I run the EF scaffolding generator (this is a db first project) I run this:

dotnet ef dbcontext scaffold "....connection..." Microsoft.EntityFrameworkCore.SqlServer -o "output" --data-annotations

When I look at the User class I get this:

public class User
{
    public User()
    {
        AreaCreatedByUser = new HashSet<Area>();
        AreaModifiedByUser = new HashSet<Area>();
        CitationCreatedByUser = new HashSet<Citation>();
        CitationModifiedByUser = new HashSet<Citation>();
    }

public Guid Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
public Guid CreatedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
public Guid? ModifiedByUserId { get; set; }
public DateTime? ModifiedDate { get; set; }

public virtual ICollection&lt;Area&gt; AreaCreatedByUser { get; set; }
[InverseProperty("ModifiedByUser")]
public virtual ICollection&lt;Area&gt; AreaModifiedByUser { get; set; }
[InverseProperty("CreatedByUser")]
public virtual ICollection&lt;Citation&gt; CitationCreatedByUser { get; set; }
[InverseProperty("ModifiedByUser")]
public virtual ICollection&lt;Citation&gt; CitationModifiedByUser { get; set; }

}

(It is actually used in hundreds of tables, but I have abbreviated the above to make it a bit clearer.)

I really don't want to navigate from a user to all the records that use a user in these audit lines, but I don't know what I can do to strip this out or prevent it from being generated. When I get a user from the database I don't want all these extra fields, even if they are null without an include. I guess if I drop the FK relationship that might do it, but that does not seem a good idea at all.

Any suggestions?

database entity-framework

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Creating a Database-First Model in Entity Framework

Entity Framework (EF) is the preferred data access solution and Object-Relational Mapper (ORM) from Microsoft. There are three methodologies you can pick from when building an EF model: Database-First, Model-First, and Code-First. In this post, we'll explore creating a Database-First model using an EDMX designer file. What is Entity Framework?

Migrate Entity Framework Core to SQL Database on Startup

This ASP.NET Core tutorial explains how to migrate Entity Framework Core to SQL Database on Startup. How to automatically migrate database changes from code in ASP.NET Core using Entity Framework Core from the Startup.cs file. Use the EF Core DB Context Service to automatically migrate database changes.

Entities relationship with Entity Framework Core

Let’s make relationships between entities with Entity Framework Core easy with Code First approach

Entity Framework Core in ASP.NET Core 3.1

We will learn about entity framework core in details i.e. what, how & why and see how to use it in ASP.NET Core Web API to implement CRUD operations. Though here we will be using entity framework in ASP.NET Core this can be implemented even in .NET Framework for your data persistence.

Unit Test Data Access Layer (Entity Framework) with in-Memor Database

Unit test Data Access Layer (Entity Framework) with in-memory database. Unit testing code that uses data from database is a difficult task. If you use Entity Framework you can stub your data with Effort - in-memory Entity Framework data provider