none
Custom Members in Entity Framework Class RRS feed

  • Question

  • I have my SQL table as below

    Usermaster:

       Userid Int Identity

       Username NVarchar(Max)

       Password NVarchar (Max)

    Hence EF5 generates a class with three members UserId, Username, Password. I want to have a class which has one more member like RepresentUsername which is a combination of UserId_Username_<SomeRandomNumber>

    Is this possible? Can some one help out with a small code snippet?

    Monday, May 13, 2013 1:31 PM

Answers

  • Hi again,

    I don't know if I am understand you at all...

    The first code snippet shown above should be more or less the file generated from EF5. You need to ensure that this class is generated as a partial class.

    If so, then you need to create another file (for example, "Usermaster.Partil.cs") with the same namespace and class name than the one automatically generated from EF5.

    If you define your own partial class in this way:

    namespace MyNamespace.Entities
    {
       public partial class Usermaster
       {
          public int UserId_Username
          {
             get 
             {
                 // here your code
             }
          }
       }
    }

    Then you only need to complete the code as you need using UserId and Username properties populated from EF5. In this way, you are just extending EF5 generated class witho your own coded properties.

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Tuesday, May 14, 2013 12:26 PM

All replies

  • Hi,

    Try to use Partial Classes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Monday, May 13, 2013 1:40 PM
  • Need a bit of example. The specified link is too much high level documentation. Can you throw a more contextual snippet?
    Monday, May 13, 2013 2:14 PM
  • Hi again,

    If EF5 generates a partial class in this way:

    namespace MyNamespace.Entities
    {
       public partial class Usermaster
       {
          public int UserId { get; set; }
          public string Username { get; set; }
          public string Password { get; set; }
       }
    }

    Then, you could extend this partial class as follows:

    namespace MyNamespace.Entities
    {
       public partial class Usermaster
       {
          public int UserId_Username
          {
             get 
             {
                 // here your code
             }
          }
       }
    }

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Tuesday, May 14, 2013 8:04 AM
  • Reyes,

    Thanks for the snippet. When the EF5 prepares the data from database would it intimate us with any event so that I can populate the custom column?

    Tuesday, May 14, 2013 12:01 PM
  • Hi again,

    I don't know if I am understand you at all...

    The first code snippet shown above should be more or less the file generated from EF5. You need to ensure that this class is generated as a partial class.

    If so, then you need to create another file (for example, "Usermaster.Partil.cs") with the same namespace and class name than the one automatically generated from EF5.

    If you define your own partial class in this way:

    namespace MyNamespace.Entities
    {
       public partial class Usermaster
       {
          public int UserId_Username
          {
             get 
             {
                 // here your code
             }
          }
       }
    }

    Then you only need to complete the code as you need using UserId and Username properties populated from EF5. In this way, you are just extending EF5 generated class witho your own coded properties.

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Tuesday, May 14, 2013 12:26 PM