locked
Entity Framework code generation RRS feed

  • Question

  • User1040882835 posted

    Someone please help me understand this insane EF idea. We all know that if one adds DataAnnotation to generated classes, they will be wiped out on next generation. I cloned each class of 25 entities. I added Metadata typeof, [Key] to my cloned partial classes. I have done everything that needs to be done so my DataAnnotations will not wiped out. I created a folder and put all my  cloned classes in it. Now my problems: 1. How do I use my classes in my controller instead of the EF classes? The EF classes seem to be the ones used. If I add a new entity, EF regenerates all the 25 entities. I have to go through all the entities  to add USING DataAnnotation and add [Key] to each class. I must be really stupid because I do not believe that I have create 25 clones for 25 entities. I don't know why my clones are not used. The entity names are the same. I cannot put my clones in the same Model folder because I get duplicate names cannot be in the same folder. I am really frustrated.

    Tuesday, March 31, 2015 1:45 AM

Answers

  • User2053451246 posted

    There is no kicker there.  Create another .cs file.  Call it campaignMetaData.  Inside that .cs file create these:

    [MetaDataType(typeof(campaignMetaData))]
    public partial class campaign {}
    
    public class campaignMetaData
    {
       // re-define properties and add data annotations to them
    }

    EDIT:  You can have classes with the same name.  But you can't have files with the same name.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2015 12:36 AM
  • User-1452742429 posted

    Hey 

    as  ryanbesko described  

     you defined [MetaDataType(typeof(campaignMetaData))] on

    Public Partial Campaign {}

    which will be combined at run time with your EF generated Class Campaign as both are partial and have same name   .Therefore all validation rules defined in Campaign MetaData are applied  on EF generated  Campaign class. 

     Code Snippet of Controller and View of my sample Example 

    using System;
    using System.Collections.Generic;
    
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Chicumbane.Web.Models;
    using System.IO;
    
    
    namespace Chicumbane.Web.Areas.Admin.Controllers
    {
        public class ArticleController : Controller
        {
    	        // GET: /Article/Create
            public ActionResult Create()
            {
                if (Session["Firstname"] != null)
                {
                ViewBag.Name = Session["Firstname"].ToString() + " " + Session["LastName"].ToString();
                
                ViewBag.article_category = new SelectList(db.tbl_category, "category_id", "category_description");
                return View();
                     }
                else
                {
                    return RedirectToAction("Login", "Pages", new { area = "Admin" });
                }
            }
    
            // POST: /Article/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            //[Bind(Include = "Article_id,Article_title,Article_text,Article_author,article_category,article_creationdate,Article_Approver,Article_ApprovalDate,Article_Status,Article_Image")]
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(tbl_article tbl_article,HttpPostedFileBase Image, HttpPostedFileBase file)
            {
             
                if (ModelState.IsValid)
                {
    
                    if (Image != null && Image.ContentLength > 0)
                    {
                        Guid Imagename = Guid.NewGuid();
                        string picture = Path.GetFileName(Image.FileName);
                        string path = Path.Combine(Server.MapPath("~/Uploads/Image"), Imagename.ToString());
                        string[] paths = path.Split('.');
    
                        Image.SaveAs(path+".jpg");
    
    
                        tbl_article.Article_Image = Imagename + ".jpg";
                    }
                    if (file != null && file.ContentLength > 0)
                    {
                        if (file.ContentType == "application/pdf")
                        {
    
                            Guid name = Guid.NewGuid();
                            string path = Path.Combine(Server.MapPath("~/Uploads/pdf"), name.ToString());
                            string[] paths = path.Split('.');
                            file.SaveAs(paths[0] + ".pdf");
                            tbl_article.Article_file = name + ".pdf";
    
    
                        }
                    }
                   
                        //add  created  by using session variable
                        tbl_article.Article_author =Convert.ToInt32(Session["UserID"].ToString());
                        tbl_article.article_creationdate = DateTime.Now;
                        tbl_article.Article_Status = 0;
                       
                    db.tbl_article.Add(tbl_article);
                    db.SaveChanges();
                    TempData["SaveMesage"] = "O artigo foi criado com sucesso";
                    return RedirectToAction("Index");
                }
    
            
                ViewBag.article_category = new SelectList(db.tbl_category, "category_id", "category_description", tbl_article.article_category);
                return View(tbl_article);
                     }
                else
                {
                    return RedirectToAction("Login", "Pages", new { area = "Admin" });
                }
            }
        }
    }
    
    

    View 

    @model Chicumbane.Web.Models.tbl_article
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>Create Article</h5>
                    </div>
                    <div class="ibox-content">
    
                        @using (Html.BeginForm("Create",
                            "Article",
                            FormMethod.Post,
                            new { enctype = "multipart/form-data", id = "myform", role = "form" }))
                        {
                            @Html.AntiForgeryToken()
    
                            <div class="form-horizontal">
                                @Html.ValidationSummary(true)
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.article_category, "Category", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.DropDownList("article_category", String.Empty)
                                        @Html.ValidationMessageFor(model => model.article_category)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_title,"Title", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.EditorFor(model => model.Article_title)
                                        @Html.ValidationMessageFor(model => model.Article_title)
    
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_Image,"Image", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        <input type="file" name="Image" id="Image" class="file-control form-control" />
    
                                        @Html.ValidationMessageFor(model => model.Article_Image)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_Image,"File", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        <input type="file" name="file" id="file" class="file-control form-control" />
    
                                        @Html.ValidationMessageFor(model => model.Article_file)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_text,"Text", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.TextAreaFor(model => model.Article_text)
                                        @Html.ValidationMessageFor(model => model.Article_text)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    <div class="col-md-offset-2 col-md-10">
    
                                        <input type="submit" value="Create" class="btn btn-primary" />
                                        @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-white" })
                                    </div>
                                </div>
                            </div>
    
                        }
                    </div>
                </div>
            </div>
        </div>
    
      

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2015 1:32 AM

All replies

  • User-821857111 posted

    I cloned each class of 25 entities.
    What does that mean? Can you provide an example of one of these cloned classes? 

    Tuesday, March 31, 2015 2:30 AM
  • User-1452742429 posted

    Hi

    Yes that's true if you applied data Annotation on Generated EF  classes it will be lost once  you update your Edmx .To use resolve this kind of   Problem Metadata Key is solution .

     Sample Code examples. All model classes resides in Models Folder. EF Generated a class tbl_article which  has all entities same as column name in database table .Now  Made this class partial  . I created another Class By name of ArticleMetaData  and Put all the clone entities of  tbl_article   which  needs DataAnnotation in this class. Now In Same Folder I created one more partial class with the same name  tbl_article which has no entities and applied meta Key on this blank partial class . So now if I regenerate Ef Classes my DataAnnotation Does not Wipe out and I used EF generated classes for data operation 

    EF Class

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Manual changes to this file may cause unexpected behavior in your application.
    //     Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace Chicumbane.Web.Models
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
    
        
        public partial class tbl_article
        {
            public int Article_id { get; set; }
            public string Article_title { get; set; }
            public string Article_text { get; set; }
            public Nullable<int> Article_author { get; set; }
            public Nullable<int> article_category { get; set; }
            public Nullable<System.DateTime> article_creationdate { get; set; }
            public Nullable<int> Article_Approver { get; set; }
            public Nullable<System.DateTime> Article_ApprovalDate { get; set; }
            public Nullable<int> Article_Status { get; set; }
            public string Article_Image { get; set; }
            public Nullable<System.DateTime> Article_modifieddate { get; set; }
            public Nullable<int> Article_modifiedby { get; set; }
            public string Article_file { get; set; }
        
            public virtual tbl_user_reg tbl_user_reg { get; set; }
            public virtual tbl_user_reg tbl_user_reg1 { get; set; }
            public virtual tbl_category tbl_category { get; set; }
            public virtual tbl_user_reg tbl_user_reg2 { get; set; }
        }
    }
    

    Class With Data Annotation

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.ComponentModel.DataAnnotations;
    
    namespace Chicumbane.Web.Models
    {
    public class ArticelMetaData
    {
    
    [Required(ErrorMessage = "Por favor Entre Título do artigo")]
    [StringLength(300)]
    [Display(Name = "Title")]
    public string Article_title { get; set; }
    [Display(Name = "Text")]
    [AllowHtml]
    public string Article_text { get; set; }
    [Display(Name = "Author")]
    public Nullable<int> Article_author { get; set; }
    [Display(Name = "Category Name")]
    [Required(ErrorMessage = "Por favor, Selecione Categoria Artigo")]
    public Nullable<int> article_category { get; set; }
    [Display(Name = "Created On")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm:ss tt}")]
    public Nullable<System.DateTime> article_creationdate { get; set; }
    [Display(Name = "Authorizer")]
    public Nullable<int> Article_Approver { get; set; }
    [Display(Name = "Approved On")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm:ss tt}")]
    public Nullable<System.DateTime> Article_ApprovalDate { get; set; }
    [Display(Name = "Status")]
    public Nullable<int> Article_Status { get; set; }
    [Display(Name = "Image")]
    public string Article_Image { get; set; }
    [Display(Name = "Modified On")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm:ss tt}")]
    public Nullable<System.DateTime> Article_modifieddate { get; set; }
    [Display(Name = "Modified By")]
    public Nullable<int> Article_modifiedby { get; set; }
    [Display(Name = "File")]
    public string Article_file { get; set; }
    }
    }

    Blank Partial Class To apply Meta Key

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    namespace Chicumbane.Web.Models
    {
        public class PartialModelClasses
        {
        }
    
       
        [MetadataType(typeof(ArticelMetaData))]
    
        public partial class tbl_article
        {
        }
     
    
    }

    Tuesday, March 31, 2015 3:04 AM
  • User1040882835 posted

    Thanks but I still don't see how the controller calls the calls ArticleMetaData.  Do I use tbl_article? Are you saying I should not create a separate folder for my classes? If I have 25 classes, do I create 25 partial classes for my DataAnnotations?  Do I create another 25 empty classes to reference my partial classes? If possible please show a snippet of controller and a view.

    Tuesday, March 31, 2015 9:42 AM
  • User1040882835 posted

    Hello Mike, I am  doing the same thing like Techbat's example except I was combining his ArticleMetaData and the Blank partial class. Please check my followup questions. When I said clone, I meant I copied all the fields from the EF class into a partial class I created for the Annotations. Thanks for the helping hand.

    Tuesday, March 31, 2015 10:00 AM
  • User2053451246 posted

    Your application will use the combined result of the EF generated class and the one you generated for your Data Annotations, as long as they are both partial classes.  That's just how partial classes work.

    If it looks like your application is not using the classes with your Data Annotations on the properties, make sure you have them in the same namespace as the EF generated classes.  If you made all of them in a separate folder chances are the namespaces do not match.

    Tuesday, March 31, 2015 11:02 AM
  • User1040882835 posted

    This is from EF

    //------------------------------------------------------------------------------
    // <auto-generated>
    // This code was generated from a template.
    //
    // Manual changes to this file may cause unexpected behavior in your application.
    // Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

    namespace WebASHPRA.Models
    {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;


    public partial class campaign
    {
    [Key]
    public int campaign_id { get; set; }
    public string description { get; set; }
    public System.DateTime start_date { get; set; }
    public Nullable<System.DateTime> end_date { get; set; }
    public string row_user { get; set; }
    public System.DateTime row_time { get; set; }
    public string Campaign_flag { get; set; }
    public string AMG_Allowed { get; set; }
    }
    }

    This is from my Folder

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;

    namespace WebASHPRA.Models.ASHPRA
    {
    [MetadataType(typeof(campaign))]
    public partial class campaign
    { }

    public partial class P_campaign
    {
    [Key]
    public int campaign_id { get; set; }
    public string description { get; set; }
    public System.DateTime start_date { get; set; }
    public Nullable<System.DateTime> end_date { get; set; }
    public string row_user { get; set; }
    public System.DateTime row_time { get; set; }
    public string Campaign_flag { get; set; }
    public string AMG_Allowed { get; set; }
    }
    }

    Okay I see the first problem is the namespace.

    Do I have to create a separate empty class for the MetaData

    So my understanding is after I take care of the name space, I have to create the same scenario for 25 classes?

    Holy not understnding!

    Thank you all

    Tuesday, March 31, 2015 1:09 PM
  • User1040882835 posted

    So I made changes to my partial with DataAnnotations. I made the namespace the same as the EF gen but when I tried to save in the Model folder I got a message "Not A;llowed"

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;

    namespace WebASHPRA.Models
    {
    [MetadataType(typeof(P_campaign))]
    public partial class campaign
    { }

    public partial class P_campaign
    {
    [Key]
    public int campaign_id { get; set; }
    [StringLength(50)]
    [Display(Name="Campaign")]
    public string description { get; set; }
    [DisplayFormat(DataFormatString="{0:MM/dd/yyyy hh:mm:ss tt}")]
    public System.DateTime start_date { get; set; }
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy hh:mm:ss tt}")]
    public Nullable<System.DateTime> end_date { get; set; }
    public string row_user { get; set; }
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy hh:mm:ss tt}")]
    public System.DateTime row_time { get; set; }
    public string Campaign_flag { get; set; }
    public string AMG_Allowed { get; set; }
    }
    }

    Tuesday, March 31, 2015 1:28 PM
  • User2053451246 posted

    They should be like my answer in this post.  Don't make the class you put your annotations in partial, and it can't be the same name as your partials.   Call it p_campaignMetaData or something.  Only the first part where you put the [MetaDataType(typeof(p_campaignMetaData)] attribute.

    http://forums.asp.net/post/5790438.aspx

    Tuesday, March 31, 2015 3:56 PM
  • User1040882835 posted

    This is what I have now. What name is used to save? campaign or campaignMetaData? I checked your post. You did not answer my other questions.

    Do I do this for all my EF Classes?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;

    namespace WebASHPRA.Models
    {
    [MetadataType(typeof(campaignMetaData))]
    public partial class campaign
    { }

    public class campaignMetaData
    {
    [Key]
    public int campaign_id { get; set; }
    [StringLength(50)]
    [Display(Name="Campaign")]
    public string description { get; set; }
    [DisplayFormat(DataFormatString="{0:MM/dd/yyyy hh:mm:ss tt}")]
    public System.DateTime start_date { get; set; }
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy hh:mm:ss tt}")]
    public Nullable<System.DateTime> end_date { get; set; }
    public string row_user { get; set; }
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy hh:mm:ss tt}")]
    public System.DateTime row_time { get; set; }
    public string Campaign_flag { get; set; }
    public string AMG_Allowed { get; set; }
    }
    }

    Thank you.

    Tuesday, March 31, 2015 11:15 PM
  • User2053451246 posted

    Globascope

    What name is used to save? campaign or campaignMetaData?

    As all tutorials show you create new records like this:

    db.EFClassName.Add(WhateverYouDecidedToCallYourVariable);
    db.SaveChanges();
    

    And you update existing ones like this:

    // edit your entity and mark it as needing saving and actually doing the saving similar to below
    db.Entry(WhatEverYouDecidedToCallYourVariable).State = EntityState.Modified;
    db.SaveChanges();

    Your Meta Data classes are only used for your Data Annotations and those get handled automatically as long as you have them set up correctly, just like in my post marked the answer in the link I posted above.  I'm not sure which questions you say I didn't answer; you didn't have your Meta Data code set up right in the first place so once you do ask your additional questions.

    But, as part of maybe what you are wanting additional answers about, only create Meta Data classes for EF entities you need to add Data Annotations to.

    But your code above does look correct now.  In saying this I assume this is a separate .cs file you created to it does not get overwritten by EF code generators.

    Wednesday, April 1, 2015 12:15 AM
  • User1040882835 posted

    When I said save as, I meant then name of my class .cs

    n saying this I assume this is a separate .cs file you created to it does not get overwritten by EF code generators.

    That is the kicker. My EF class is called campaign. I cannot save my class also name campaign in the same .edmx. I get error message. In other words what name do I give my class?

    Thank you for your patience.

    Wednesday, April 1, 2015 12:28 AM
  • User2053451246 posted

    There is no kicker there.  Create another .cs file.  Call it campaignMetaData.  Inside that .cs file create these:

    [MetaDataType(typeof(campaignMetaData))]
    public partial class campaign {}
    
    public class campaignMetaData
    {
       // re-define properties and add data annotations to them
    }

    EDIT:  You can have classes with the same name.  But you can't have files with the same name.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2015 12:36 AM
  • User1040882835 posted

    So magically, my controller knows that when I reference campaign it goes to campaignMetaData.

    Can I do the same for my imported Stored Procedures?

    Why does EF issue error message about missing Keys for generated classes eventhough they are keys. I always have to edit the EF class to add DataAnnotation and [Key]

    Thank you.

    Wednesday, April 1, 2015 12:53 AM
  • User2053451246 posted

    It's not magic.  You've told it what to do because you have [MetaDataType(typeof(campaignMetaData))] on your partial camgpaign class.  And partial classes "inherit" each other.

    EDIT:  I can't answer your stored procedure question.  I use those to do intense record manipulation in the database, not return records.

    EDIT: I'm not sure why you would get that keys error message.  My guess is the database is not set up correctly.

    Wednesday, April 1, 2015 12:55 AM
  • User-1452742429 posted

    Hey 

    as  ryanbesko described  

     you defined [MetaDataType(typeof(campaignMetaData))] on

    Public Partial Campaign {}

    which will be combined at run time with your EF generated Class Campaign as both are partial and have same name   .Therefore all validation rules defined in Campaign MetaData are applied  on EF generated  Campaign class. 

     Code Snippet of Controller and View of my sample Example 

    using System;
    using System.Collections.Generic;
    
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Chicumbane.Web.Models;
    using System.IO;
    
    
    namespace Chicumbane.Web.Areas.Admin.Controllers
    {
        public class ArticleController : Controller
        {
    	        // GET: /Article/Create
            public ActionResult Create()
            {
                if (Session["Firstname"] != null)
                {
                ViewBag.Name = Session["Firstname"].ToString() + " " + Session["LastName"].ToString();
                
                ViewBag.article_category = new SelectList(db.tbl_category, "category_id", "category_description");
                return View();
                     }
                else
                {
                    return RedirectToAction("Login", "Pages", new { area = "Admin" });
                }
            }
    
            // POST: /Article/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            //[Bind(Include = "Article_id,Article_title,Article_text,Article_author,article_category,article_creationdate,Article_Approver,Article_ApprovalDate,Article_Status,Article_Image")]
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(tbl_article tbl_article,HttpPostedFileBase Image, HttpPostedFileBase file)
            {
             
                if (ModelState.IsValid)
                {
    
                    if (Image != null && Image.ContentLength > 0)
                    {
                        Guid Imagename = Guid.NewGuid();
                        string picture = Path.GetFileName(Image.FileName);
                        string path = Path.Combine(Server.MapPath("~/Uploads/Image"), Imagename.ToString());
                        string[] paths = path.Split('.');
    
                        Image.SaveAs(path+".jpg");
    
    
                        tbl_article.Article_Image = Imagename + ".jpg";
                    }
                    if (file != null && file.ContentLength > 0)
                    {
                        if (file.ContentType == "application/pdf")
                        {
    
                            Guid name = Guid.NewGuid();
                            string path = Path.Combine(Server.MapPath("~/Uploads/pdf"), name.ToString());
                            string[] paths = path.Split('.');
                            file.SaveAs(paths[0] + ".pdf");
                            tbl_article.Article_file = name + ".pdf";
    
    
                        }
                    }
                   
                        //add  created  by using session variable
                        tbl_article.Article_author =Convert.ToInt32(Session["UserID"].ToString());
                        tbl_article.article_creationdate = DateTime.Now;
                        tbl_article.Article_Status = 0;
                       
                    db.tbl_article.Add(tbl_article);
                    db.SaveChanges();
                    TempData["SaveMesage"] = "O artigo foi criado com sucesso";
                    return RedirectToAction("Index");
                }
    
            
                ViewBag.article_category = new SelectList(db.tbl_category, "category_id", "category_description", tbl_article.article_category);
                return View(tbl_article);
                     }
                else
                {
                    return RedirectToAction("Login", "Pages", new { area = "Admin" });
                }
            }
        }
    }
    
    

    View 

    @model Chicumbane.Web.Models.tbl_article
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>Create Article</h5>
                    </div>
                    <div class="ibox-content">
    
                        @using (Html.BeginForm("Create",
                            "Article",
                            FormMethod.Post,
                            new { enctype = "multipart/form-data", id = "myform", role = "form" }))
                        {
                            @Html.AntiForgeryToken()
    
                            <div class="form-horizontal">
                                @Html.ValidationSummary(true)
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.article_category, "Category", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.DropDownList("article_category", String.Empty)
                                        @Html.ValidationMessageFor(model => model.article_category)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_title,"Title", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.EditorFor(model => model.Article_title)
                                        @Html.ValidationMessageFor(model => model.Article_title)
    
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_Image,"Image", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        <input type="file" name="Image" id="Image" class="file-control form-control" />
    
                                        @Html.ValidationMessageFor(model => model.Article_Image)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_Image,"File", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        <input type="file" name="file" id="file" class="file-control form-control" />
    
                                        @Html.ValidationMessageFor(model => model.Article_file)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Article_text,"Text", new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.TextAreaFor(model => model.Article_text)
                                        @Html.ValidationMessageFor(model => model.Article_text)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    <div class="col-md-offset-2 col-md-10">
    
                                        <input type="submit" value="Create" class="btn btn-primary" />
                                        @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-white" })
                                    </div>
                                </div>
                            </div>
    
                        }
                    </div>
                </div>
            </div>
        </div>
    
      

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2015 1:32 AM
  • User1040882835 posted

    Thank you all for the patience in explaining the what and how to work with EF code generation. I think it's too much work for the developer to do this. I hope Microsoft and the EF team can come up with a better way like allowing developer to apply DataAnnotation to EF class but preserving the work during regeneration and then re-apply them after regeneration. Thanks again. Laughing

    Wednesday, April 1, 2015 10:06 AM
  • User-821857111 posted

    Future versions of EF won't include support for the EDMX files.

    http://blogs.msdn.com/b/adonet/archive/2014/10/21/ef7-what-does-code-first-only-really-mean.aspx

    You are encouraged to learn the Code First approach, which at least means that your annotations won't be wiped out. Or you should use the EF power tools addon for VS to generate your model classes from the database.

    Wednesday, April 1, 2015 10:40 AM