Entity Framework Core: Combine owned properties with inheritance

.net .net-core c# entity-framework entity-framework-core

Question

Given are the following classes:

public class Rule
{
    public long Id { get; set; }
    public string Filter { get; set; }
    public RuleAction Action { get; set; }
}

public abstract class RuleAction
{
}

public class RuleAction1 : RuleAction
{
    public string Value { get; set; }
}

public class RuleAction2 : RuleAction
{
    public decimal Percent { get; set; }
}

I want to map these classes to the following table layout. I use Entity Framework Core Preview 2.

Table "Rule"
  - Id
  - Filter
  - ActionDiscriminator
  - Value // only set if the object in Action is typeof(RuleAction1)
  - Percent // only set if the object in Action is typeof(RuleAction2)

The important part is that "Action" is not mapped to a separate table. I know i can map the property as a "Owned property" like described in this article (OwnsOne): https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-ef-core-2-0-preview-2/ but this dosn't seem to work in combination with inheritance, at least i couldn't find an example.

Anybody knows how to combine owned properties with inheritance?

1
0
8/8/2017 12:47:53 AM

Popular Answer

Could you just do something like this:

 public class RuleAction1 : RuleAction
    {
        public string Value { get; set; }
        public decimal Percent { get; set; } = null;
    }

    public class RuleAction2 : RuleAction
    {
        public decimal Percent { get; set; }
        public string Value { get; set; } = null;
    }

That way those value match the table schema but just default to null values. Or you could do something like this:

    public abstract class RuleAction
    {
        public string Value { get; set; } = null;
        public decimal Percent { get; set; } = null;
    }

    public class RuleAction1 : RuleAction
    {            
    }

    public class RuleAction2 : RuleAction
    {            
    }

I could be way off, sorry if this is only slowing you down.

1
8/9/2017 12:07:22 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow