Early EF Experience

Source: Internet
Author: User

The first development tool used is the Vs2013,sqlserver Management Studio2012.

Entity type:

1[Table ("Student", schema="dbo")]//database table name2 //Properties3 #regionProperties4 5[Column ("StudentID", typename="Int")]6 [key,required]7  Public intid{Get;Set;}8 9[Column ("STUDENTNBR", typename="NVarchar")]Ten[MaxLength ( -), Required] One  Public stringstudentnumber{Get;Set;}
 

Create the DbContext object class Stucontext

1  Public classstucontext:dbcontext{2      PublicStucontext ()3:Base(Sqlhelper.getconnectionstring ()) {4    }    5          /// <summary>6         ///Create a new DbContext object.7         /// </summary>8         /// <returns>DbContext Object</returns>9          Public Staticstucontextcreatenew () {Ten             return NewStucontext (); One         } A  -         #regionDbSet Definition -           the         #regionStudent -           -         /// <summary> -         ///Get student DbSet. +         /// </summary> -           PublicDbset<student> students{Get;Set;} +          A          /// <summary> at         ///Get student model by ID. -         /// </summary> -         /// <param name= "Student Id" >Student Id</param> -         /// <returns>Part Model</returns> -          PublicStudent Getstudent (intStudentID) { -             returnStudents.firstordefault (m = m.id = =StudentID); in         } -        to          /// <summary> +         ///Get Studentmodel by student number. -         /// </summary> the         /// <param name= "Studentnumber" >Studnumber</param> *         /// <returns>Student Model</returns> $          PublicStudent GetPart (stringStudentnumber) {Panax Notoginseng             returnParts.where (m = M.studentnumber = =studentnumber) -                         . FirstOrDefault (); the         } +  A         /// <summary> the         ///ADD Studentmodel to context. +         /// </summary> -         /// <param name= "Stumodel" >Student Model</param> $          Public voidaddstudent (Student stumodel) { $             if(Stumodel = =NULL) -                 Throw NewArgumentNullException ("Stumodel"); -  the Students.add (Stumodel); -         }Wuyi  the         /// <summary> -         ///Remove student model from context. Wu         /// </summary> -         /// <param name= "Stumodel" >Student Model</param> About          Public voidremovestudent (Student stumodel) { $ Students.remove (Stumodel); -         } -         #endregion -      A      +         #endregion the}
View Code

Test

1 using(Stucontext db =NewStucontext ()) {2     3Student s=NewStudent () {4Studentnumber="10001",5Studentname="Aware",6     7 ...8               }9 Ten db. Addstudent (s); One db. SaveChanges (); A}

Early EF Experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.