locked
modify how "EntityRef"s are displayed - LINQ class RRS feed

  • Question

  • User-1712241964 posted

    Im using a C# LINQ class and have done some modifying of table and object names via a MetaData.cs file(outside of generated code files), but would like to know if there are any examples of how to do the same for auto-generated EntityRefs?

    Any ideas or suggestions would be greatly appreciated!

     

    Tuesday, January 6, 2009 3:40 PM

Answers

  • User-1005219520 posted

    Entity Framework (EF) and Linq to SQL ( L2S ) are both based on LINQ - It looks like you are using L2S.  I'm guessing your MetaData.cs file contains partial classes of the data context.

    You can use the DisplayColumn attribute to change the displayed EntityRef column. The snippet below changes the Customer EntityRef to display the phone number.

     

    [DisplayColumn("Phone")]
    [MetadataType(typeof(CustomerMD))]
    public partial class Customer {
        public class CustomerMD {
            [ScaffoldColumn(false)]
            public object Fax;
        }
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 6, 2009 4:32 PM
  • User-330204900 posted

    To get the field on a particular form to show a different name you use the DisplayNameAttribute on that column not the table: 

    <MetadataType(GetType(Order.OrderMD))> _
    Partial Public Class [Order]
        Public Class [OrderMD]
            'EntityRef
            Private newPropertyValue As Object
            <DisplayName("My Customer")> _
            Public Property Customer() As Object
                Get
                    Return newPropertyValue()
                End Get
                Set(ByVal value As Object)
                    newPropertyValue = value()
                End Set
            End Property
            Public Employee As Object
        End Class
    End Class
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 7, 2009 9:44 AM

All replies

  • User-1005219520 posted

    Entity Framework (EF) and Linq to SQL ( L2S ) are both based on LINQ - It looks like you are using L2S.  I'm guessing your MetaData.cs file contains partial classes of the data context.

    You can use the DisplayColumn attribute to change the displayed EntityRef column. The snippet below changes the Customer EntityRef to display the phone number.

     

    [DisplayColumn("Phone")]
    [MetadataType(typeof(CustomerMD))]
    public partial class Customer {
        public class CustomerMD {
            [ScaffoldColumn(false)]
            public object Fax;
        }
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 6, 2009 4:32 PM
  • User-1712241964 posted

    I don't want to change which column is displayed.     I am just wanting to change the name, as displayed in the UI.
    for example:
    I use the "DisplayName" attribute on an object(standard column) so that the 'ApplicationName' column renders to the user as 'Application Name' (just adding a space).
    Adding a "DisplayName" attribute for my SCIncident column is pointless though, because(regarding updates and inserts) it is not displayed, rather a dropdown box labeled "YNResp"(the PK in the FK relatiopnship) is displayed. 

    here is an example of an entityref from my auto-generated code:

    private
    EntityRef<YNResp> _YNResp;

    Furthermore, in my DB, this "YNResp" is the primary used by several foreign keys throughout my DB.
    I might have table 1, with a "Foo" field that is a FK to the PK in the "YNResp" table and want the name displayed next to the rendered dropdown box to be "Foos".
    I might have table 2, with a "Widget" field that is also a FK to the PK in the "YNResp" table and want the name displayed next to thoise rendered dropdown boxes to be "Widgets".

    SO what I am wanting to know(and I'm still going on the presumption that my MetaData.cs file is still the place to do it), is how to change the label for the dropdown box for this PK DIFFERENTLY for each use of it in the multiple tables?

     

    Wednesday, January 7, 2009 9:08 AM
  • User-330204900 posted

    To get the field on a particular form to show a different name you use the DisplayNameAttribute on that column not the table: 

    <MetadataType(GetType(Order.OrderMD))> _
    Partial Public Class [Order]
        Public Class [OrderMD]
            'EntityRef
            Private newPropertyValue As Object
            <DisplayName("My Customer")> _
            Public Property Customer() As Object
                Get
                    Return newPropertyValue()
                End Get
                Set(ByVal value As Object)
                    newPropertyValue = value()
                End Set
            End Property
            Public Employee As Object
        End Class
    End Class
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 7, 2009 9:44 AM
  • User-1712241964 posted

      Sigh.   the answer couldn't have been simpler.

    Thank you!

    Wednesday, January 7, 2009 3:00 PM