How to implement a table strategy for a specific type using an entity structure

I map a set of tables that have a common set of fields:

alt text

So, as you can see, I'm using a table type strategy to match inheritance.

But...

I could not associate them with an abstract type containing these common properties.

Can I do this with EF?


BONUS: The only documented one Entity Data Model Mapping Scenariois Table-per-concrete-type inheritance http://msdn.microsoft.com/en-us/library/cc716779.aspx : P

+5
source share
5 answers

Finally, I created the "Iggy" interface , which contains accessors for common properties:

public Interface Iggy
{
  string modifiedBy { get; set; }
  DateTime modifiedDate { get; set; }
}

public partial class Al:Iggy{}
public partial class Ben:Iggy{}
public partial class Carl:Iggy{}

# , , -, :)

+5

?!

+1

. , , , , EDMX: -

  • , , . CSDL. CSDL.
  • C-S . , .
  • , PK, . Id IsNull = false.

, , , , ( ), .

, : -)

0

, , (), . , T4, EF , POCO .

, ISimpleAuditable, , T4 , , , .

Include.tt :

GetTypeInterfaces (EntityType entity)   {       string interfaces = String.Empty;

    if(IsNome(entity))
    {
        if(IsIdentifiableNumeric(entity))
            interfaces = "IEntity<int>";

        if(IsIdentifiableText(entity))
            interfaces = "IEntity<string>";

        if (interfaces == String.Empty)
            interfaces = "INome";

        if(IsSimpleAuditable(entity))
            if (interfaces==String.Empty) 
                interfaces = "ISimpleAuditable<string>";
            else
                interfaces += ", ISimpleAuditable<string>";
    }
    else
    {
        if(IsIdentifiableNumeric(entity))
            interfaces = "IIdentifiable<int>";

        if(IsIdentifiableText(entity))
            interfaces = "IIdentifiable<string>";

        if(IsSimpleAuditable(entity))
            if (interfaces==String.Empty) 
                interfaces = "ISimpleAuditable<string>";
            else
                interfaces += ", ISimpleAuditable<string>";
    }
    if (interfaces != string.Empty)
        if (entity.BaseType !=null)
            interfaces = string.Format(", {0}", interfaces); 
        else
            interfaces = string.Format(": {0}", interfaces); 

    return interfaces;
}

T4 :

<#@ template language="C#" debug="true" hostspecific="true"#> 
<#@ import namespace="System.Diagnostics" #>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ include file="Winsys.Sandstone.Data.ttinclude"#><#@ 
 output extension=".cs"#><#

const string inputFile = @"Winsys.Sandstone.Data.edmx";
var textTransform = DynamicTextTransformation.Create(this);
var code = new CodeGenerationTools(this);
    var ef = new MetadataTools(this);
var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
var fileManager = EntityFrameworkTemplateFileManager.Create(this);
var itemCollection = new EdmMetadataLoader(textTransform.Host, TextTransform.Errors).CreateEdmItemCollection(inputFile);
var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);

if     (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
{
return string.Empty;
}

WriteHeader(codeStringGenerator, fileManager);

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
fileManager.StartNewFile(entity.Name + ".cs");
BeginNamespace(code);
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
<#
    var propertiesWithDefaultValues =     typeMapper.GetPropertiesWithDefaultValues(entity);
    var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
    var complexProperties = typeMapper.GetComplexProperties(entity);

    if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
    {
#>
    public <#=code.Escape(entity)#>()
    {
<#
        foreach (var edmProperty in propertiesWithDefaultValues)
        {
#>
        this.<#=code.Escape(edmProperty)#> =         <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
<#
        }

        foreach (var navigationProperty in collectionNavigationProperties)
        {
#>
        this.<#=code.Escape(navigationProperty)#> = new     List<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
<#
        }

        foreach (var complexProperty in complexProperties)
        {
#>
        this.<#=code.Escape(complexProperty)#> = new         <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
        }
#>
    }

<#
    }

var simpleProperties = typeMapper.GetSimpleProperties(entity);
if (simpleProperties.Any())
{
    foreach (var edmProperty in simpleProperties)
    {
#>
<#=codeStringGenerator.Property(edmProperty)#>
<#
    }
}

if (complexProperties.Any())
{
#>

<#
    foreach(var complexProperty in complexProperties)
    {
#>
<#=codeStringGenerator.Property(complexProperty)#>
<#
    }
}

var navigationProperties = typeMapper.GetNavigationProperties(entity);
if (navigationProperties.Any())
{
#>
<#=WinsysGenerator.GetSimpleAuditable(entity)#>
<#
    foreach (var navigationProperty in navigationProperties)
    {
#>
<#=codeStringGenerator.NavigationProperty(navigationProperty)#>
<#
    }
}
#>

, T4,

0

@SDReyes . . , . . :

(TPC): , . , , .

http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx

@SDReyes :

public abstract class Iggy
{
  string modifiedBy { get; set; }
  DateTime modifiedDate { get; set; }
}

public class Al:Iggy{}
public class Ben:Iggy{}
public class Carl:Iggy{}

We can leave the definitions of Al, Ben, and Carl empty. All inherited fields from Iggy will be automatically taken from the Iggy definition into a separate table for each class.

0
source

All Articles