Adding Custom Fields to User Entity

CodeXennial 21 Reputation points
2021-09-06T04:07:04.483+00:00

I am using this tutorial to add a couple fields to the user table. In my code I have the following lines:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<ApplicationUser>()
        .Property(e => e.RegistrationDate);

    builder.Entity<ApplicationUser>()
        .Property(e => e.RegistrationIPAddress);

    builder.Entity<ApplicationUser>()
        .Property(e => e.Banned);
}

I have looked up lambda expression which I believe is what this is, but I'm not understanding what these lines do, and why they are required, i.e.:

  1. What is the point of the RegistrationDate line and what is it doing?
  2. Do I need to add these lines for every new field I add to the user table?

This is my first time working with Entity Framework and all of the documents are getting a little confusing.

Thanks.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,346 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2021-09-06T09:19:56.883+00:00

    For ASP.NET, see the following Identity model customization in ASP.NET Core.

    In general, for EF Core, builder.Entity<ApplicationUser>().Property(e => e.RegistrationDate); is describing a table property using a builder pattern.

    Some other examples

    Tell EF Core that a column named ModifiedDate has a default value for new records.

    entity.Property(e => e.ModifiedDate).HasDefaultValueSql("(getdate())");  
    

    Describing relations between two tables.

    entity.HasOne(d => d.ContactTypeIdentifierNavigation)  
        .WithMany(p => p.Customers)  
        .HasForeignKey(d => d.ContactTypeIdentifier)  
        .HasConstraintName("FK_Customers_ContactType");  
    

    For more indepth information see Creating and configuring a model

    In a complex model it's prudent to break out each table configuration as shown below in the attached image.

    129536-ef-config.png

    Do I need to add these lines for every new field I add to the user table?

    Yes

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2021-09-06T09:22:38.71+00:00

    Hi @CodeXennial ,

    What is the point of the RegistrationDate line and what is it doing?

    Entity Framework Core uses a set of conventions to build a model based on the shape of your entity classes. we can specify additional configuration to supplement and/or override what was discovered by convention via the Data annotations or Fluent API.

    By using the above lambda expression in the OnModelCreating method, it is the Fluent API method to configure the class and its properties. In the lambda expression, for example, we can call the relate method to configure the relationship between tables or configure the property is required.

    You can refer the following sample:

    internal class MyContext : DbContext  
    {  
        public DbSet<Blog> Blogs { get; set; }  
    
        #region Required  
        protected override void OnModelCreating(ModelBuilder modelBuilder)  
        {  
            modelBuilder.Entity<Blog>()  
                .Property(b => b.Url)  
                .IsRequired();   
        }  
        #endregion  
    }  
    
    public class Blog  
    {  
        public int BlogId { get; set; }  
        public string Url { get; set; }  
    }   
    

    By using the above code, we can use the IsRequired method to make the url is required when insert new entity. You can also use the data annotations to configure a model, code like this:

    using System.ComponentModel.DataAnnotations;  
    using Microsoft.EntityFrameworkCore;  
      
    namespace EFModeling.DataAnnotations.Required  
    {  
        internal class MyContext : DbContext  
        {  
            public DbSet<Blog> Blogs { get; set; }  
        }  
      
        #region Required  
        public class Blog  
        {  
            public int BlogId { get; set; }  
      
            [Required]  
            public string Url { get; set; }  
        }  
        #endregion  
    }  
    

    In this scenario, we can see that there is no need to add the lambda expression in the OnModelCreating method.

    Do I need to add these lines for every new field I add to the user table?

    No, if you want to specify additional configuration to supplement and/or override what was discovered by convention, you can add the lambda expression in the OnModelCreating method. Otherwise, there is no need to use them.

    More detail information, you can refer the following articles:

    Creating and configuring a model

    Fluent API in Entity Framework Core

    Besides, to learn EF Core with Asp.net Core, I suggest you could refer the following tutorials:

    Entity Framework Core

    OverView of Entity Framework Core

    EF Core With MVC


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    2 people found this answer helpful.