views:

30

answers:

2

I am setting up an entity model where I have a common base entity for most of the other entities. For example:

Person -> BaseEntity

BaseEntity Id Created etc...

Person FirstName LastName ect...

In the corresponding storage table all the columns are in the Person table.

Person Id Created FirstName LastName etc...

When I go to map the entity properties in the Person entity I want to map the Id column in the Person table to the Id property in EntityBase and the FirsName column to the FirstName entity property.

However, when you are on the Person entity and add the Person table for mapping you can only map the properties in the Person entity not those in the inherited class?

Have I setup something wrong conceptually?

Thanks

A: 

I think that what you are trying to do is impossible in EF. Maybe I am wrong. I did it with implementing common properties by defining interfaces:

public interface ICreationInfo
{
    DateTime CreationDate { get; set; }
    User CreatedBy { get; set; }
    EntityReference<User> CreatedByReference { get; set; }
}

public interface ILastModificationInfo
{
    DateTime LastModificationDate { get; set; }
    User LastModificationBy { get; set; }
    EntityReference<User> LastModificationByReference { get; set; }
}

Then I defined T4 template to have it in every entity class:

<#@ template language="C#" #>
<#@ output extension="cs" #>
<#@ import namespace="System.Collections.Generic" #>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;

<# 
 String[] classNames = new String[] {"User","Project","Group","GroupUser","OperationSystem","TaskType","Priority","Severity","Status","Version","Platform","Task","TaskUser","Attachment","Comment","Setting","CustomField"}; 
 List<String> classWithoutModificationInfo = new List<String>(); 
 classWithoutModificationInfo.Add("GroupUser");
 classWithoutModificationInfo.Add("TaskUser");
 classWithoutModificationInfo.Add("Attachment");
#>
namespace CamelTrap.Models
{
<# foreach(String className in classNames) { #>
 public partial class <#= className #> : IBasicEntityInfo, ICreationInfo <#= (!classWithoutModificationInfo.Contains(className))?",ILastModificationInfo":"" #>
 {   

 }
<# } #>
}

This generates code:

public partial class User : IBasicEntityInfo, ICreationInfo, ILastModificationInfo
{

}
public partial class Project : IBasicEntityInfo, ICreationInfo, ILastModificationInfo
{

}
public partial class Group : IBasicEntityInfo, ICreationInfo, ILastModificationInfo
{

}

When I want to access basic properties, I check interface.

LukLed
A: 

The key here is to have a base class that isn't an Entity in the Conceptual Model.

This approach isn't supported in EF 1.0 (3.5 SP1) or EF 4.0 Beta 2.

But in EF 4.0 RTM you will be able to take classes like this:

public class Base
{
  public DateTime CreatedDate{get;set;}
}
public class Derived:Base
{
   public string SomeProperty {get;set;}
}

and map the 'Derived' class to a Entity that looks like this in the Conceptual model (random made up entity DSL!):

entity Derived{
   DateTime CreateDate;
   string SomeProperty;
}

To do this you will need to write the classes yourself (using POCO or Code-Only) rather than letting EF generate them for you.

Hope this helps

Alex

Alex James