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