Question

I have amended the MyContext.tt T4 template so that all generated POCOs implement IDataEntity:

<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#=codeStringGenerator.EntityClassOpening(entity)#> : IDataEntity
{ ... }

Is it possible to generate POCOs so that they implement a generic IDataEntity<T> where T is the type of the primary key column for the given table?

For example, if the primary key of the Customer table is a Guid, then the generated class would be:

public class Customer : IDataEntity<Guid>
{ ... }

I am using Entity Framework 6.

Was it helpful?

Solution 2

In the MyContext.tt, I made the following changes:

Added a new method to the CodeStringGenerator class:

public string PropertyTypeName(EdmProperty edmProperty)
{
    return _typeMapper.GetTypeName(edmProperty.TypeUsage);
}

Added the following declaration:

<#
// Determine the type if the "Id" column for the DomainEntity<T> base type
var idProperty = typeMapper.GetSimpleProperties(entity).SingleOrDefault(p => p.Name.Equals("Id"));
#>

And in the class declaration:

<#=codeStringGenerator.EntityClassOpening(entity)#> : IDataEntity<<#=codeStringGenerator.PropertyTypeName(idProperty)#>>

OTHER TIPS

I'm using something like this:

<#@ Import Namespace="System" #>
<#@ Import Namespace="System.Data" #>
<#@ Import Namespace="System.Data.SqlClient" #>

...

<#
    DataSet primaryKeys = GetPrimaryKeys();
    string primaryKeyName = GetPrimaryKey(primaryKeys, codeStringGenerator.EntityClassOpening(entity));

    public class <#=code.Escape(entity)#> : IDataEntity<<#= primaryKeyName #>>
#>

...

<#+
    string GetPrimaryKey(DataSet data, string tableName)
    {
        if (data != null && data.Tables != null && data.Tables[0].Rows != null)
        {
            foreach (DataRow row in data.Tables[0].Rows)
            {
                if ((row[0] as string) == tableName)
                {
                    return (row[1] as string);
                }
            }
        }
        return null;
    }

    DataSet GetPrimaryKeys()
    {
        string connectionString = "...";
        SqlConnection connection = new SqlConnection(connectionString);
        string getAllExtendedProperties = @"              
                SELECT table_name, column_name
                FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
                WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1";

        SqlCommand sqlSelectCommand = new SqlCommand(getAllExtendedProperties, connection);
        try
        {
            SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
            mySqlDataAdapter.SelectCommand = sqlSelectCommand;
            DataSet data = new DataSet();
            if (connection.State != ConnectionState.Open) connection.Open();
            mySqlDataAdapter.Fill(data);
            return data;
        }
        catch (Exception ex)
        {
            //...
            return null;
        }
        finally
        {
            connection.Close();
        }
    }    
#>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top