asp.net mvc 3 - Entity Framework Database First with proper use of Data Anotations -


i have 1 project ef , code first approach , there using of data annotations straight forward. i'm working database first , see using data annotations more specific want know right steps implement it.

the structure of project provides data access this:

dataaccess

in modelextensions files i've created add data annotations dbcontextmodel.tt entities.

here structure of 1 of files in modelextensions:

using system; using system.componentmodel.dataannotations; using system.linq; using system.text;  namespace dataaccess.modelextensions {     [metadatatype(typeof(mcs_contenttypesmetadata))]     public partial class mcs_contenttypes : baseentity     {      }      internal sealed class mcs_contenttypesmetadata     {         [required]         [stringlength(10)]         public string name { get; set; }     } } 

i have several questions here. first - namespace. should namespace dataaccess.modelextensions or have remove .modelextensions part. looking @ project using db first , there namespace dataaccess not sure why needed (if so). - need add other references dbcontextmodel.tt entities? use standard c# classes , rename them : public partial class mcs_contenttypes : baseentity. have use special approach creating explicitly expose connection between entity , file?

1) namespace of extension models must same namespace of ef auto-generated entity classes - if namespace of dbcontextmodel.tt entity classes dataaccess, should set namespace of classes dataaccess.

2) doesn't question completely, in approach, names of entity classes , classes must same.

the following example shows should be. suppose ef generates following entity class you:

namespace yoursolution {     using system;     using system.collections.generic;      public partial class news     {         public int id { get; set; }         public string title { get; set; }             } } 

so, partial classes should following:

namespace yoursolution {     [metadatatype(typeof(newsattribs))]     public partial class news     {         // leave empty.     }      public class newsattribs     {                     // attribs come here.          [display(name = "news title")]         [required(errormessage = "please enter news title.")]         public string title { get; set; }          // , other properties want...     } } 

so, doesn't need : baseentity inheritance.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -