📙
Learn EF Core 9
  • Introduction of Entity Framework Core
  • EF Core - AI Tools
  • What's New in EF Core 5
    • Simple Logging
    • Filtered Included
    • Backing Fields
    • Keyless Entity Types
    • Configure Precision and Scale
    • Translation of Contains on byte arrays
    • Many-to-many Relationship
    • Table-per-type (TPT) mapping
    • Required one-to-one Dependents
    • Support for Fields using Lambda
    • Drop Column from SQLite Database
    • Index Attribute
  • BulkExtensions in EF Core
  • Connection Strings: Entity Framework Core
  • Entity Framework Core Model
  • DbContext
  • DbSet
  • Relationship in EF-Core
  • Lazy Loading in EF Core
  • Migrations in EF-Core
  • Handling Concurrency in EF-Core
  • Raw SQL Queries in EF-Core
  • Database Providers
    • SQL Server
    • SQLite
    • InMemory
    • Cosmos
    • PostgreSQL
  • Project Types
    • Console
    • MVC
    • WinForm
    • Xamarin
    • Blazor
Powered by GitBook
On this page
  1. What's New in EF Core 5

Support for Fields using Lambda

PreviousRequired one-to-one DependentsNextDrop Column from SQLite Database

Last updated 3 years ago

Support for Fields using Lambda

EF Core 5 allows you to use the lambda methods in the ModelBuilder for fields as well as properties. Let's suppose you don't want to use properties for some reason and decide to use public fields.

public class Book
{
    public int Id;
    public string Title;
    public string Category;
    public int AuthorId;
    public Author Author;
}

public class Author
{
    public int Id;
    public string Name;
    public ICollection<Book> Books;
}

In EF Core 5, you can map these fields using the lambda builders as shown below.

public class EntityContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder opBuilder)
    {
        opBuilder.UseSqlServer("Data Source=(localdb)\\ProjectsV13;Initial Catalog=BookContextDb;");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Author>(b =>
        {
            b.Property(e => e.Id);
            b.Property(e => e.Name);
        });

        modelBuilder.Entity<Book>(b =>
        {
            b.Property(e => e.Id);
            b.Property(e => e.Title);
            b.Property(e => e.Category);
            b.Property(e => e.AuthorId);
            b.HasOne(e => e.Author).WithMany(e => e.Books);
        });
    }

    public DbSet<Author> Blogs { get; set; }
    public DbSet<Book> Posgs { get; set; }
}

It will create the following tables on the SQL Server.

CREATE TABLE [dbo].[Authors] (
    [Id]   INT            IDENTITY (1, 1) NOT NULL,
    [Name] NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_Authors] PRIMARY KEY CLUSTERED ([Id] ASC)
);

CREATE TABLE [dbo].[Books] (
    [Id]       INT            IDENTITY (1, 1) NOT NULL,
    [AuthorId] INT            NOT NULL,
    [Category] NVARCHAR (MAX) NULL,
    [Title]    NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_Books] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Books_Authors_AuthorId] FOREIGN KEY ([AuthorId]) REFERENCES [dbo].[Authors] ([Id]) ON DELETE CASCADE
);

Before EF Core 5, if you try to map these fields using the model builder, It will throw the following exception.

System.ArgumentException: 'The expression 'e => e.Id' is not a valid property expression. The expression should represent a simple property access: 't => t.MyProperty'. (Parameter 'propertyAccessExpression')'
Improve EF Core performance with EF Extensions