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: }
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);
}
}
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>
Contributor
2231 Points
3706 Posts
Looking for example regarding calculated or computed Columns in Entity Framework Code First
Sep 14, 2016 03:09 PM|sudip_inn|LINK
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
thanks
All-Star
17652 Points
3510 Posts
Re: Looking for example regarding calculated or computed Columns in Entity Framework Code First
Sep 16, 2016 05:34 AM|Chris Zhao|LINK
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.
reference: https://msdn.microsoft.com/en-sg/data/jj591583.aspx#DatabaseGenerated
Best Regards,
Chris
Contributor
2231 Points
3706 Posts
Re: Looking for example regarding calculated or computed Columns in Entity Framework Code First
Sep 16, 2016 02:02 PM|sudip_inn|LINK
sorry @Chris just do not understand how to work with computed columns
please provide me a easy example. thanks
All-Star
17652 Points
3510 Posts
Re: Looking for example regarding calculated or computed Columns in Entity Framework Code First
Sep 19, 2016 03:24 AM|Chris Zhao|LINK
Hi sudip_inn,
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
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