DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Entity Framework Core Supports Constructors With Arguments

Entity Framework Core Supports Constructors With Arguments

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

Gunnar Peipman user avatar by
Gunnar Peipman
·
Jun. 03, 19 · Web Dev Zone · Tutorial
Like (2)
Save
Tweet
11.25K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Entity Framework Database Framework

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Modernizing Testing With Data Pipelines
  • Pattern Matching for Switch
  • How to Leverage Method Chaining To Add Smart Message Routing in Java
  • Adding Authentication to a Web Application with Auth0, React, and JWT

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo