locked
Looking for example regarding calculated or computed Columns in Entity Framework Code First RRS feed

  • Question

  • User264732274 posted

    suppose my model has full name property but table has no full name field. so tell me how to have full name computed property which will return first name and last name jointly as full name. i got a sample code from this link http://geekswithblogs.net/DavidPaquette/archive/2012/09/23/calculated-columns-in-entity-framework-code-first-migrations.aspx but do not understand how to use it in code

    just guide me how to use it

       1: public string FullName 
    
       2: {
    
       3:     get { return string.Format("{0} {1}", FirstName, LastName); }
    
       4: }

    thanks

    Wednesday, September 14, 2016 3:09 PM

All replies

  • User-2057865890 posted

    Hi sudip_inn,

    If you're mapping your Code First classes to tables that contain computed columns, you don't want Entity Framework to try to update those columns. But you do want EF to return those values from the database after you've inserted or updated data. You can use the DatabaseGenerated annotation to flag those properties in your class along with the Computed enum.

    using(UserContext context = new UserContext())
    {
         var user = context.Users.ToList();
    
         foreach (var item in user)
         {
             Console.WriteLine(item.FullName);
         }
    }

    reference: https://msdn.microsoft.com/en-sg/data/jj591583.aspx#DatabaseGenerated

    Best Regards,

    Chris

    Friday, September 16, 2016 5:34 AM
  • User264732274 posted

    sorry @Chris just do not understand how to work with computed columns

    please provide me a easy example. thanks

    Friday, September 16, 2016 2:02 PM
  • User-2057865890 posted

    Hi sudip_inn,

    sudip_inn

    please provide me a easy example

    The article you mentioned above (http://geekswithblogs.net/DavidPaquette/archive/2012/09/23/calculated-columns-in-entity-framework-code-first-migrations.aspx) has a sample. Or you could download Contoso University Web Application.

    Person class

    public abstract class Person
    {
         public int ID { get; set; }
    
         [Required]
         [StringLength(50)]
         [Display(Name = "Last Name")]
         public string LastName { get; set; }
         [Required]
         [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
         [Column("FirstName")]
         [Display(Name = "First Name")]
         public string FirstMidName { get; set; }
    
         [Display(Name = "Full Name")]
         public string FullName
         {
             get
             {
                 return LastName + ", " + FirstMidName;
             }
         }
    }

    Student Index View

    <table class="table">
        <tr>
            <th>
                @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
            </th>
            <th>
                First Name
            </th>
            <th>
                Full Name
            </th>
            <th>
                @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
            </th>
            <th></th>
        </tr>
    
    
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstMidName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FullName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.EnrollmentDate)
                </td>            
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                    @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                </td>
            </tr>
        }
    
    </table>

    Best Regards,

    Chris

    Monday, September 19, 2016 3:24 AM