CSC439: Visual Programming: Models
CSC439: Visual Programming: Models
Models
Fall 2018
1 Model
Models
Entity model
View model
DTO (Data Transfer Objects)
2 Model
Entity Model
An entity model is typically used to define a table in a
database
public class Product
{
//Auto-Implemented Properties
public int Id { get; set; }
public string Name { get; set; }
public int Category { get; set; }
public int Status { get; set; }
} Id name Category Status
1 Potato 1 0
2 Tomato 1 1
3 Model
View Model
A view model is used to transport data from the controller to
the view
View model typically contains other data than the entity
model, although some properties are the same
The view model needs some properties from the entity model,
but could also need other information from another database
table
A view model is never used to directly update the database
To update the database the data from the view model is added
to an entity model, which then in turn updates the
database table
4 Model
Example
//Model
public class Product
{
//Auto-Implemented Properties
public int Id { get; set; }
public string Name { get; set; }
public int Category { get; set; }
public int Status { get; set; }
}
//ViewModel
public class ProductDetailsViewModel
{
//Auto-Implemented Properties
public string Name { get; set; }
public int Category { get; set; }
public string Availablility { get; set; }
}
5 Model
Example
//Controller
public class ProductController : Controller
{
private readonly DataContext _context;
public ProductController(DataContext dataContext)
{
_context = dataContext;
}
public ActionResult Details(int id)
{
var m = _context.Product.FirstOrDefault();
//Object Initializer using Property
ProductDetailsViewModel vm = new ProductDetailsViewModel
{
Name = m.Name,
Category = m.Category,
Availablility = m.Status==0?"Not Available":"Available"
};
return View(vm);
}
}
6 Model
Data Annotation
Data annotations are attributes you add to properties in a
model, to enforce rules about them
You can specify that a field is required or must have a
maximum number of characters.
You can specify one annotation per code line, or multiple
annotations as a comma-separated list inside a set of square
brackets.
[Required]
[MaxLength(80)]
Or
[Required, MaxLength(80)]
7 Model
Example
namespace MvcMusicStore.Models
{
public class Album
{
public int AlbumId { get; set; }
public int ArtistId { get; set; }
[Required(ErrorMessage = "An Album Title is required")]
[StringLength(160)]
public string Title { get; set; }
[StringLength(1024)]
public string AlbumArtUrl { get; set; }
}
}
8 Model
Entity Framework
Entity Framework is an object-relational mapper (O/RM)
(O/RM) is a programming technique for converting data
between incompatible type systems
in relational databases and object-oriented
programming languages
It enables .NET developers to work with a database using
.NET objects
It eliminates the need for most of the data-access code
that developers usually need to write
9 Model
Working with Entity Framework and Db
Creating Database in the server
Install nuget package (NuGet is a free and open-source package
manager designed for the Microsoft development platform)
Microsoft.AspNetCore.App (2.1.1)
Microsoft.EntityFrameworkCore (2.1.4)
Microsoft.EntityFrameworkCore.SqlServer (2.1.4)
Microsoft.EntityFrameworkCore.Tools (2.1.4)
Add database server name to AppSettings.json
"ConnectionStrings": {
"DefaultConnection": "Data Source=FACULTY-PC;Initial
Catalog=DemoForCsc439db;User ID=test; Password=aa"
},
Create Entity Models inside Models folder
10 Model
Working with Entity Framework and Db
Add a data context class that inherits from the DbContext class.This
class will be the context that you use to interact with the database.
//base class constructor call
public dataContext(DbContextOptions<dataContext> options)
: base(options)
{
}
For all the tables in the database, the table’s entity class must be added as a
DbSet property in the context class
11 Model
Working with Entity Framework and Db
Write controller code to handle interaction between model and
view
public IActionResult List()
{
var test = _context.Students;
var model = new List<Students>(); //Generics
foreach (var i in test)
{
var s = new Students();
s.Id = i.Id;
s.Name = i.Name;
model.Add(s);
}
return View(model);
}
12 Model
Working with Entity Framework and
Migration
Install nuget package
Microsoft.EntityFrameworkCore (2.1.4)
Microsoft.EntityFrameworkCore.Tools (2.1.4)
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
Add database server name to AppSettings.json
"ConnectionStrings": {
"DefaultConnection": "Server=Name;Database=dbname;Trusted_Connection=True;"
},
Add a data context class that inherits from the DbContext class.This class
will be the context that you use to interact with the database.
//base class constructor call
public dataContext(DbContextOptions<dataContext> options)
: base(options)
{
}
13 Model
Working with Entity Framework and
Migration
For all the tables in the database, the table’s entity class must
be added as a DbSet property in the context class
public DbSet<ViewRecordProject.Models.Students>
Students { get; set; }
Configure startup class by importing
Microsoft.EntityFrameworkCore and add service
AddDbContext inside ConfigureServices method
services.AddDbContext<dataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("
DefaultConnection")));
Run Migration Codes
add-migration Initial
Update-database
14 Model