2

For example, lets say I had this object that I wanted to put into a SQL database with EntityFramework:

public class Shift
{
    public Guid Id { get; set; }

    public Attendance Attendance { get; set; }
    public Managers Managers { get; set; }

    // other properties
}

public class Attendance
{
    public int AM { get; set; }
    public int MidDay { get; set; }
    public int PM { get; set; }
}

public class Managers
{
    public string AM { get; set; }
    public string MidDay { get; set; }
    public string PM { get; set; }
}

Normally, I would figure this cannot be done, and I would just flatten everything into a simple collection of properties:

public class Shift
{
    public Guid Id { get; set; }

    public int Attendance_AM { get; set; }
    public int Attendance_MidDay { get; set; }
    public int Attendance_PM { get; set; }

    public string Managers_AM { get; set; }
    public string Managers_MidDay { get; set; }
    public string Managers_PM { get; set; }
}

But is there a way to make EF store the complex object as a single table?

EDIT

This might help illustrate my question.

The goal is to have both code blocks above, work with the same Table in SQL: The goal table

7
  • Why do you care that it's stored as a single table? That is to say... If you're using an ORM, it's so that you can stop worrying about the implementation detail. The classes you've defined could easily fit in a multi-table model. Commented Mar 3, 2017 at 20:23
  • A few reasons: 1) Its something that I've thought about a few times in EF, but never found a good answer. 2) It seems ridiculous to me to create a whole table just to store one or two points of data, and then have to have SQL join them when I need them. 3) Logically, all this data should be in the same table, as it constitutes one single entity. 4) (the big one) I'm working with an existing DB schema. Commented Mar 3, 2017 at 20:28
  • Not sure I follow. Do you have a separate table in your existing DB schema? It would be better if you show the tables and ask how to map them in EF then. Commented Mar 3, 2017 at 20:30
  • I'll make a table and edit with a screenshot to help illustrate. Commented Mar 3, 2017 at 20:33
  • 1
    But if you ask for so called Complex Types, they are not supported yet in EF Core, but were in EF6 and will be in Core later. Commented Mar 3, 2017 at 20:35

3 Answers 3

3

Add the following in the OnModelCreating() of your context class. It works with .NET Core 2.0 only:

 modelBuilder.Entity<Shift>().OwnsOne(s => 
             { cb.OwnsOne(c => c.Attendance);
               cb.OwnsOne(c => c.Managers);
             });
Sign up to request clarification or add additional context in comments.

1 Comment

What is cb? This variable is not defined in your answer or in the original question.
1

This feature will be available in Entity Framework Core 2. Currently Entity Framework Core 2 is in Preview, but the feature is not complete in Preview 1, but can instead be used by using their nightly builds.

You can read about it here: https://blogs.msdn.microsoft.com/dotnet/2017/05/12/announcing-ef-core-2-0-preview-1/

(The feature is already available for "regular" Entity Framework)

Comments

0

I am very supprised to face this kind of problem in .net world. Java Hibernate provides very easy solution for this.

So thanks to comments and Bashir M. Saad answer I find a solution.

You should add [Owned] annotation at the top of nested object without adding primary key. For more information please visit. https://learn.microsoft.com/en-us/ef/core/modeling/owned-entities

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.