Custom Properties in partial class of Entities Framework object not displayed in ASP.NET Dynamic Data
-
Hello, I am trying to display in a Dynamic Data app, a custom property added to the partial class of an ADO.NET Entity Framework entity class. For example, where Person is a entity in a ADO.NET Entity Framework model, I would add:
public partial class Person
{
public string FullName { get { return FirstName + " " + LastName; } }
}However, with Dynamic Data, it uses the internal System.Web.DynamicData.ModelProviders.EFTableProvider for generating its columns collection which only allows properties defined in the entity model to be displayed - as in only the database columns, not custom added ones. Does anyone know any work arounds to this, or is it possible and I am missing something obvious. Seems silly I can't do this. LINQ to SQL with Dynamic Data can do this easy as it uses reflection in its DLinqTableProvider to find all properties in the object, not just those with mappings to database fields. EFTableProvider's enumerator to create its columns for Dynamic Data:
foreach (EdmMember member in entityType.get_Members()) // only entity DB mapped fields are returned in the Members property
DLinqTableProvider's enumerator to create its columns for Dynamic Data:IEnumerator<PropertyInfo> enumerator = this.GetOrderedProperties(rowType.get_Type()).GetEnumerator()
where GetOrderedProperties returns a list of properties based on reflection:PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
Anyone have any thoughts on how to do this effectively? I need the many-to-many relationship management that Entity Framework provides - otherwise I would stick with LINQ to SQL. Thanks for any help! Cheers, kris -
Hello, I am trying to display in a Dynamic Data app, a custom property added to the partial class of an ADO.NET Entity Framework entity class. For example, where Person is a entity in a ADO.NET Entity Framework model, I would add:
public partial class Person
{
public string FullName { get { return FirstName + " " + LastName; } }
}However, with Dynamic Data, it uses the internal System.Web.DynamicData.ModelProviders.EFTableProvider for generating its columns collection which only allows properties defined in the entity model to be displayed - as in only the database columns, not custom added ones. Does anyone know any work arounds to this, or is it possible and I am missing something obvious. Seems silly I can't do this. LINQ to SQL with Dynamic Data can do this easy as it uses reflection in its DLinqTableProvider to find all properties in the object, not just those with mappings to database fields. EFTableProvider's enumerator to create its columns for Dynamic Data:
foreach (EdmMember member in entityType.get_Members()) // only entity DB mapped fields are returned in the Members property
DLinqTableProvider's enumerator to create its columns for Dynamic Data:IEnumerator<PropertyInfo> enumerator = this.GetOrderedProperties(rowType.get_Type()).GetEnumerator()
where GetOrderedProperties returns a list of properties based on reflection:PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
Anyone have any thoughts on how to do this effectively? I need the many-to-many relationship management that Entity Framework provides - otherwise I would stick with LINQ to SQL. Thanks for any help! Cheers, krisWhy are you adding the custom property? If it is to use in the DisplayColumn metadata attribute, you can override ToString on your entity to return the full name, and remove the DisplayColumn attribute, and the ToString value will be used instead for FK displays.
-
Why are you adding the custom property? If it is to use in the DisplayColumn metadata attribute, you can override ToString on your entity to return the full name, and remove the DisplayColumn attribute, and the ToString value will be used instead for FK displays.
I am trying to acheive the same goal. However, when I try and access one of the properties from the entity model that I have configured I get the following error message: Server Error in '/' Application. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1061: 'Contracts.CONTRACT_PERIOD' does not contain a definition for 'start_date' and no extension method 'start_date' accepting a first argument of type 'Contracts.CONTRACT_PERIOD' could be found (are you missing a using directive or an assembly reference?) Source Error: Line 39: StringBuilder sb = new StringBuilder(); Line 40: sb.Append("("); Line 41: sb.Append(this.start_date.ToShortDateString()); Line 42: sb.Append(" - "); Line 43: sb.Append(this.end_date.ToShortDateString()); Source File: c:\Documents and Settings\...\My Documents\Visual Studio 2010\Projects\...\Contracts\App_Code\Metadata.cs Line: 41
-
Hello, I am trying to display in a Dynamic Data app, a custom property added to the partial class of an ADO.NET Entity Framework entity class. For example, where Person is a entity in a ADO.NET Entity Framework model, I would add:
public partial class Person
{
public string FullName { get { return FirstName + " " + LastName; } }
}However, with Dynamic Data, it uses the internal System.Web.DynamicData.ModelProviders.EFTableProvider for generating its columns collection which only allows properties defined in the entity model to be displayed - as in only the database columns, not custom added ones. Does anyone know any work arounds to this, or is it possible and I am missing something obvious. Seems silly I can't do this. LINQ to SQL with Dynamic Data can do this easy as it uses reflection in its DLinqTableProvider to find all properties in the object, not just those with mappings to database fields. EFTableProvider's enumerator to create its columns for Dynamic Data:
foreach (EdmMember member in entityType.get_Members()) // only entity DB mapped fields are returned in the Members property
DLinqTableProvider's enumerator to create its columns for Dynamic Data:IEnumerator<PropertyInfo> enumerator = this.GetOrderedProperties(rowType.get_Type()).GetEnumerator()
where GetOrderedProperties returns a list of properties based on reflection:PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
Anyone have any thoughts on how to do this effectively? I need the many-to-many relationship management that Entity Framework provides - otherwise I would stick with LINQ to SQL. Thanks for any help! Cheers, kris