Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom DateTime-Range class which is used in several models:

public class Entity 
{
   //...
   public DateTimeRange Usage {get; set; }
   //....
}

public class DateTimeRange
{
   public DateTime From { get; set; }
   public DateTime To { get; set; }
}


How can I achieve that the properties are store in the same database table as the parent entity (TPH)?

For the code below I want this datatable:

Id | Name | Validity_From | Validity_To | Usage_From | Usage_To
1 | Hummer | 2015-01-01 | 2999-12-31 | 2021-09-01 | 2021-09-15

I couldn't find a solution for this specific case in the EF Core documentation (TPH/TPT).

I found a solution in the Microsoft docs:
Owned Entity Types - EF Core | Microsoft Docs[^]

In this approach I had to do the mapping myself. I'm looking for a general mapping. If this owned entity is used in 30 models, I don't want to mapp this in all models.

Note:
Using DateTimeOffset is not an option.
The class property could also be something else like a phone object (phone.pre, phone.number).


What I have tried:

using System;

namespace Foo
{
    public class Testdata
    {
        public void Test()
        {
            //SqLite-Context..simplified...
            dynamic dbContext = new Object();

            var car = new Car()
            {
                Name = "Hummer",
                Validity = new() { From = new(2015, 1, 1), To = new(2999, 12, 31) },
                Usage = new() { From = new(2021, 9, 1), To = new(2021, 9, 15) }
            };

            dbContext.Cars.Add(car);
            dbContext.SaveChanges();
        }
    }

    public class Car
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTimeRange Validity { get; set; }
        public DateTimeRange Usage { get; set; }
    }

    public class DateTimeRange
    {
        public DateTime From { get; set; }
        public DateTime To { get; set; }
    }
}
Posted
Updated 24-Sep-21 1:12am
v3
Comments
Afzaal Ahmad Zeeshan 24-Sep-21 6:02am    
And what is the current table structure that EF Core gives?
Sni.DelWoods 24-Sep-21 6:10am    
Actually none. :-) The default behaviour would be mapping to a table named DateTimeRange and ends in an error, because there is no Id property in the DateTimeRange model.

1 solution

Solved.
The anontation <owned> does the whole job:

[Owned]
public class DateTimeRange
{
    public DateTime From { get; set; }
    public DateTime To { get; set; }
}


Output datatable is as exptected:
Validity_From = ...
Validity_To = ...

Deep dive:
Reducing Complexity Using Entity Framework Core Owned Types[^]A
Owned Entity Types - EF Core | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900