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?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

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.

Database Design Tutorial - How to Design & Plan Database for Beginners

Learn how to design and plan a database for beginners. This database design course will help you understand database concepts and give you a deeper grasp of database design. Database design is the organisation of data according to a database model. The designer determines what data must be stored and how the data elements interrelate. With this information, they can begin to fit the data to the database model.

Build a CRUD App with ASP.NET MVC and Entity Framework

Let’s walk through creating a basic application that allows the creation, reading, updating, and deletion of data (CRUD) with Entity Framework by your users while managing them easily with Okta.