{{announcement.body}}
{{announcement.title}}

Entity Framework Core Supports Constructors With Arguments

DZone 's Guide to

Entity Framework Core Supports Constructors With Arguments

In this post, we show you how to use Entity Framework Core and entity constructors with arguments.

· Web Dev Zone ·
Free Resource

Keeping objects complete and valid all the time is a strategy used in different methodologies. It's perhaps most popular in Domain Driven Design (DDD). Entity Framework Core 2.1 took a big step forward on supporting entities that don't have default empty constructors. This blog post shows how to use Entity Framework Core and entity constructors with arguments.

Using Constructors With Arguments

Let's take simple Product entity that uses constructor arguments. I'll keep it minimal for demo purposes.

public class Product : BaseEntity
{

    public int Id { get; set; }
    public int TenantId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ProductCategory Category { get; internal set; }

    public Product(int id, int tenantId, string name, string description, ProductCategory category)
    {
        Id = id;
        TenantId = tenantId;
        Name = name;
        Description = description;
        Category = category;
    }
}

The code here builds with no errors. but problems begin when we try to query data from the database. This line of code asks DbContext for product with id 1.

var product = _context.Products.FirstOrDefault(p => p.Id == 1);  

And here's the result.

Image title

Navigational properties are currently not supported as constructor arguments by Entity Framework Core.

We can play it around by introducing two constructors. The private one is for Entity Framework Core and the public one is for developers who create new instances of a product.

public class Product : BaseEntity
{

    public int Id { get; set; }
    public int TenantId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ProductCategory Category { get; internal set; }

    private Product(int id, int tenantId, string name, string description)
    {
        Id = id;
        TenantId = tenantId;
        Name = name;
        Description = description;
    }

    public Product(int id, int tenantId, string name, string description, 
            ProductCategory category) : this(id, tenantId, name, description)
    {
            Category = category;
    }
}

Notice how I made Entity Framework Core constructor private. This way there's no danger of developers who are writing domain entities using this constructor and forgetting to assign it a product category.

Wrapping Up

By supporting contructors with arguments, Entity Framework Core makes it easier to write code where entities are always valid. Although navigational properties are not supported in constructor argument lists yet, we were able to work around this by making a special constructor for Entity Framework Core and leaving the classic one for developers who are working on internals of entities.

Topics:
web dev ,entity framework core ,c# tutorial ,c# frameworks

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}