0% found this document useful (0 votes)
128 views

Entity Framework / Validation Model: (Key) Databasegeneratedoption - Identity) )

The document describes how to use data annotations in an Entity Framework model class to validate properties and configure relationships between entities. Key points include: 1) Data annotations like [Required], [EmailAddress], [Range] can be used to validate property values meet certain criteria. 2) Navigation properties can be configured as virtual to enable lazy loading. 3) The [Key] annotation marks a property as the primary key. [DatabaseGenerated] sets key generation to identity. 4) [InverseProperty] configures relationships between entities. [NotMapped] excludes a property from the model. 5) Razor views are strongly typed and use Html helpers to generate forms and display values. Sections

Uploaded by

ghar_dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Entity Framework / Validation Model: (Key) Databasegeneratedoption - Identity) )

The document describes how to use data annotations in an Entity Framework model class to validate properties and configure relationships between entities. Key points include: 1) Data annotations like [Required], [EmailAddress], [Range] can be used to validate property values meet certain criteria. 2) Navigation properties can be configured as virtual to enable lazy loading. 3) The [Key] annotation marks a property as the primary key. [DatabaseGenerated] sets key generation to identity. 4) [InverseProperty] configures relationships between entities. [NotMapped] excludes a property from the model. 5) Razor views are strongly typed and use Html helpers to generate forms and display values. Sections

Uploaded by

ghar_dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Entity Framework / Validation Model

namespace MyApp.Model {
public class Employee
{ Database primary key
[Key]
[DatabaseGeneratedAttribute(
Key is automatically generated by
DatabaseGeneratedOption.Identity)]
incrementing a counter
public int EmployeeId { get; set; }

[Required] Property value cannot be null / empty


public string FirstName { get; set; }

[Required]
public string FamilyName { get; set; } Property value must be formatted as
an email address
[EmailAddress]
public string Email { get; set; }
Property value must match the
supplied regular expression
[RegularExpression("\\d+\\.\\d+\\.\\d+\\.\\d+",
ErrorMessage = "Invalid IP address format")]
public string IpAddress { get; set; } Declare navigation properties to be
virtual if you want lazy loading
public virtual Department Department { get; set; }
This list contains all Messages with an
[InverseProperty("Author")] Author property that is this Employee
public virtual ICollection<Message> Messages { get; set; }

public bool IsFullTime { get; set; }

Property value must be between 1


[Range(1, 12)]
and 12 (inclusive)
public int BirthMonth { get; set; }
This property is a timestamp used for
[Timestamp]
optimistic concurrency control
public byte[] RowVersion { get; set; }

This property is to be ignored by


[NotMapped]
Entity Framework
public string FullName
{
get { return FirstName + " " + FamilyName; }
}
}
}
Import namespace into Razor view

Razor View
@using MyApp.Models
Declare a typed View
@model Employee

@{
Use a master layout / template
Layout = "~/Views/_Base.cshtml";
ViewBag.Title = "Employee Information";
} Set a property of the ViewBag
dynamic object
<h2>Employee Information</h2>
<p>These are the details of @Model.FullName:</p>
@using (Html.BeginForm())
Write out a value
{
@Html.AntiForgeryToken()
@Html.ValidationSummary() Create a HTML <form> tag
<div>
First Name: @Html.TextBoxFor(x => x.FirstName) Generate a token to prevent Cross-
</div> Site-Forgery-Requests (CSFRs)
<div>
Family Name: @Html.TextBoxFor(x => x.FamilyName)
</div> Show any validation errors
<div>
Birth Month: @Html.TextBoxFor(x => x.BirthMonth)
Create Text-boxes for Properties of
</div> the Model
<div>
IP Address: @Html.TextBoxFor(x => x.IpAddress)
</div>
<div>
Full Time:
@Html.DropDownListFor( Create a Drop-down list for IsFullTime
x => x.IsFullTime, property of the model
new [] {
new SelectListItem {
Text = "Full Time",
Value = "true",
Selected = Model.IsFullTime
},
new SelectListItem {
Text = "Part Time",
Value = "false",
Selected = !Model.IsFullTime
}
})
</div>
<input type="submit" value="Save Changes" />
} Razor comments

@* Don’t show the messages section if there are no messages *@

@if (Model.Messages != null && Model.Messages.Count() > 0)


{
<p>Messages for @Model.FullName:</p> If Statement
<ul>
@foreach (var message in Model.Messages)
{ Loop
<li>@message.MessageId</li>
}
</ul> Manually create a URL to use in a link
}

<a href="@Url.Action("Index")">List All Employees</a> Create a link referring to the Index


@Html.ActionLink("List All Employees", "Index") action of the same controller
@Html.ActionLink("Go to Home", "Index", "Home")
@Html.ActionLink("View Employee 1", "Details", new { id = 1 }, null)
@Html.Raw("&nbsp;")
Create a link referring to the Index
action of the HomeController
@section Author {
This page created by Ben. Create a link referring to the Details
} action with route/model parameters

Output a string directly as raw HTML,


without any escaping
Template
<html> Define a section (to use in a template)
<title>@ViewBag.Title</title>
<body>
<h1>My App</h1>
<div>
Render the main content of the file
@RenderBody()
that is using this template
</div>
<div>
Switch back to plain HTML text when
@if (IsSectionDefined("Author"))
in a section of C# code
{
@: Author Information:
@RenderSection("Author")
Render a section that was defined
using @section
}
</div>
</body>
</html>
Controller
...
Contains the .Includes(…) extension
using System.Data.Entity;
method used for Eager loading
namespace MyApp.Controllers
{
[Authorize]
A user must be logged in to use the
Actions on this controller
public class EmployeeController : Controller
{

An exception to the need for


[AllowAnonymous]
authorization (no need to login)
public ActionResult Index()
{
using (var context = new MyAppContext())
{
var emps = context.Employees
.Include(x => x.Messages).Include(x => x.Department)
.ToList(); Eager loading for view rendering
return View(emps); (when there is no DbContext)
}
}
Show the strongly typed View
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] matching this Action (Index.cshtml)
[Authorize(Roles = "Administrator")]
public ActionResult Delete(int id) Accept HTTP verbs GET and POST
{
using (var context = new MyAppContext())
{
var employee = new Employee { EmployeeId = id };
context.Employees.Attach(employee);
Require the user to belong to the
context.Employees.Remove(employee);
Administrator Role
context.SaveChanges();
return RedirectToAction("Index");
} Redirect the user to the Index action
}
Use this action for GET requests (i.e.,
[HttpGet]
to retrieve an empty/current form)
public ActionResult Edit(int id)
{
using (var context = new MyAppContext())
{
var emp = context.Employees
.Include(x => x.Messages).Include(x => x.Department)
.Where(x => x.EmployeeId == id).Single();
return View(emp);
}
Use this action when the user POSTs
}
back a form (i.e., saves changes)

Check for the token created by


@Html. AntiForgeryToken()
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, Employee employee)
{
Use HTML Get/Post parameters to
if (ModelState.IsValid)
construct an Employee object
{
if (employee.IpAddress == "127.0.0.1")
{
ModelState.AddModelError("", "Do not use 127.0.0.1");
return View(employee);
}
Were there any validation errors
when creating the Employee object?
else
{
using (var context = new MyAppContext())
{
var oldEmployee = Context.Employees.Find(id);
UpdateModel(oldEmployee); Add a custom validation error for
context.SaveChanges(); @Html.ValidationSummary()
return View("Details", employee);
}
}
Use HTML Get/Post parameters to
}
update the Employee object
else
{
Return the Details strongly typed
return View(employee);
view (Details.cshtml)
}
}
}
}

You might also like