{"id":1059,"date":"2012-09-08T23:08:51","date_gmt":"2012-09-08T23:08:51","guid":{"rendered":"http:\/\/nestor.nestasoft.fr\/?p=1059"},"modified":"2012-10-19T04:53:02","modified_gmt":"2012-10-19T04:53:02","slug":"asp-net-mvc-4-seconde-partie-ajout-dun-model-2","status":"publish","type":"post","link":"https:\/\/nestasoft.fr\/?p=1059","title":{"rendered":"ASP.NET MVC 4 [Seconde partie] : Ajout d&rsquo;un model (Compl\u00e9t\u00e9)"},"content":{"rendered":"<p><a href=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/ApresCreatyionAdherent.PNG\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\"  border=\"0\" alt=\"ASP NET MVC4\" src=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/ApresCreatyionAdherent.PNG\" width=\"600\" height=\"225\" \/><\/a><\/p>\n<ol>\n<li><strong><font color=\"#4f81bd\"><a href=\"http:\/\/nestor.nestasoft.fr\/?p=1012\" target=\"_blank\">ASP.NET MVC 4 [Premi\u00e8re partie]&#160;&#160; : Introduction<\/a><\/font><\/strong> <\/li>\n<li><strong><font color=\"#4f81bd\">ASP.NET MVC 4 [Deuxi\u00e8me partie]&#160; : Ajout d&rsquo;un model<\/font> <\/strong><\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Troisi\u00e8me partie]&#160; : Ajout d&rsquo;un contr\u00f4leur<\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Quatri\u00e8me partie] : Ajout d&rsquo;une vue<\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Cinqui\u00e8me partie] : Page Inspector<\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Sixi\u00e8me partie]&#160;&#160;&#160;&#160;&#160; : Razor ou ASPX<\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Septi\u00e8me partie]&#160;&#160; : Conclusion<\/font> <\/li>\n<\/ol>\n<p>Dans cette partie, nous ajouterons un fichier Model dans notre projet GCS MVC. <\/p>\n<p>Dans un projet MVC, le mod\u00e8le d\u00e9crit les donn\u00e9es manipul\u00e9es par l&rsquo;application. Il assure la gestion et garantit leur int\u00e9grit\u00e9 de ces donn\u00e9es. En gros le mod\u00e8le repr\u00e9sente tout le comportement de l&rsquo;application : le traitements des donn\u00e9es, les interactions avec la base de donn\u00e9es,\u2026<\/p>\n<p>Dans le cas d\u2019une base de donn\u00e9e, le mod\u00e8le offre des m\u00e9thodes pour manipuler les donn\u00e9es : insertion, suppression, modification, r\u00e9cup\u00e9ration. <\/p>\n<p>Passons aux choses s\u00e9rieuses <img decoding=\"async\" style=\"border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none\" class=\"wlEmoticon wlEmoticon-smile\" alt=\"Sourire\" src=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/05\/wlEmoticon-smile.png\" \/><\/p>\n<p><!--more--><\/p>\n<ul>\n<li>Ajout d\u2019un nouveau fichier dans le projet GCS &#8211; Cr\u00e9ation de la classe Adherent <\/li>\n<\/ul>\n<p>\n<a href=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/05\/Model1.PNG\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Model1\" border=\"0\" alt=\"Model1\" src=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/05\/Model1.PNG\" width=\"600\" height=\"416\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<ul>\n<li>Ajout de propri\u00e9t\u00e9s : AdherentID, AdherentTitre, AdherentGenre, AdherentContributeur, AdherentDateAbonnement. <\/li>\n<\/ul>\n<p><pre class=\"brush:csharp\"> \r\nusing System; \r\nusing System.Collections.Generic; \r\nusing System.Data.Entity; \r\nusing System.Linq\r\n\r\nnamespace ListDeProjets.Models \r\n{ \r\n    public class Adherent \r\n    { \r\n        public int AdherentID { get; set; } \r\n        public string AdherentTitre { get; set; }        \r\n        public string AdherentGenre { get; set; } \r\n        public string AdherentContributeur { get; set; } \r\n        public DateTime AdherentDateAbonnement{ get; set; }        \r\n    }\r\n}\r\n<\/pre>\n<p><\/p>\n<p>La classe Adherent r\u00e9pr\u00e9sente nos adh\u00e9rents dans notre base de donn\u00e9es. Chaque instance de cette classe correspond \u00e0 une entr\u00e9e (ligne) et chaque propri\u00e9t\u00e9 une colonne.<\/p>\n<p>  <\/p>\n<ul>\n<li>Ajoutons la classe AdherentDbContext <\/li>\n<\/ul>\n<pre class=\"brush:csharp\"> \r\n    public class AdherentDbContext: DbContext \r\n    { \r\n        public DbSet< Adherent > Adherents { get; set; } \r\n    }\r\n<\/pre>\n<p>\nLa classe Adherent au complet sera alors :<\/p>\n<pre class=\"brush:csharp\"> \r\nusing System; \r\nusing System.Collections.Generic; \r\nusing System.Data.Entity; \r\nusing System.Linq\r\n\r\nnamespace ListDeProjets.Models \r\n{ \r\n    public class Adherent \r\n    { \r\n        public int AdherentID { get; set; } \r\n        public string AdherentTitre { get; set; }        \r\n        public string AdherentGenre { get; set; } \r\n        public string AdherentContributeur { get; set; } \r\n        public DateTime AdherentDateAbonnement{ get; set; }        \r\n    }\r\n\r\n    public class AdherentDbContext: DbContext \r\n    { \r\n        public DbSet< Adherent > Adherents { get; set; } \r\n    } \r\n}\r\n\r\n<\/pre>\n<p>\nCette classe (AdherentDbContext) repr\u00e9sente le contexte de la base de donn\u00e9e Entity Framework Adherent. C\u2019est elle qui g\u00e8re la recherche, le stockage, la mise \u00e0 jour ,\u2026 des instances de classes Adherent dans la base de donn\u00e9es.<\/p>\n<p>Cette classe d\u00e9rive de la classe de base DbContext. N\u2019oublier pas d\u2019ajouter la ligne suivante dans le fichier en question (n\u00e9cessaire pour l\u2019utilisation du DbContext et DbSet).<\/p>\n<pre  class=\"brush:csharp\"> \r\nusing System.Data.Entity;\r\n<\/pre>\n<p>&#160;<\/p>\n<ul>\n<li>Pour terminer cette partie, cr\u00e9ons la chaine de connexion n\u00e9cessaire pour travailler avec notre base de donn\u00e9es. <\/li>\n<\/ul>\n<pre  class=\"brush:csharp\"> \r\n<add name=\"DefaultConnection\" \r\nconnectionstring=\"Data Source= (LocalDb)\\v11.0;Initial Catalog=CSMvc4;Integrated Security=SSPI;AttachDBFilename|DataDirectory|\\aspnet-CSMvc4-20120908130527.mdf\" providername=\"System.Data.SqlClient\" \/>\r\n<\/pre>\n<p>\u00c0 tr\u00e8s bient\u00f4t pour la troisi\u00e8me partie.<\/p>\n<p>Be connected!<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1059\" class=\"pvc_stats all  \" data-element-id=\"1059\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/nestasoft.fr\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET MVC 4 [Premi\u00e8re partie]&#160;&#160; : Introduction ASP.NET MVC 4 [Deuxi\u00e8me partie]&#160; : Ajout d&rsquo;un model ASP.NET MVC 4 [Troisi\u00e8me partie]&#160; : Ajout d&rsquo;un contr\u00f4leur ASP.NET MVC 4 [Quatri\u00e8me partie] : Ajout d&rsquo;une vue ASP.NET MVC 4 [Cinqui\u00e8me partie] : Page Inspector ASP.NET MVC 4 [Sixi\u00e8me partie]&#160;&#160;&#160;&#160;&#160; : Razor ou ASPX ASP.NET MVC 4 [Septi\u00e8me [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1059\" class=\"pvc_stats all  \" data-element-id=\"1059\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/nestasoft.fr\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":1163,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1059","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/1059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1059"}],"version-history":[{"count":12,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/1059\/revisions"}],"predecessor-version":[{"id":1124,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/1059\/revisions\/1124"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/media\/1163"}],"wp:attachment":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}