EF Learning Note 01----------Creating a database using Code First mode

Source: Internet
Author: User

1 namespaceEfdemo22 {3     usingSystem;4     usingSystem.Data.Entity;5     usingSystem.Linq;6     usingSystem.Collections.Generic;7 8      Public classEntities:dbcontext9     {Ten         //your context has been configured to be from your application's configuration file (app. config or Web. config) One         //Use the "Model1" connection string. By default, this connection string is for the LocalDb instance on your A         //"Efdemo2. Model1 "database.  -         //  -         //if you want to target other databases and/or database providers, modify the "Model1" in the application configuration file the         //The connection string.  -          Publicentities () -:Base("name=entity") -         { +         } -  +         //add DbSet for each entity type that you want to include in the model. about configuring and using the Code first model A         //For more information, seehttp://go.microsoft.com/fwlink/?LinkId=390109.  at  -          Public VirtualDbset<student> Student {Get;Set; } -          Public VirtualDbset<classes> Classes {Get;Set; } -     } -     //Class Table -      Public classClasses in     { -          Public intClassesid {Get;Set; } to          Public stringClassName {Get;Set; } +          Public VirtualIcollection<student> Student {Get;Set; } -     } the     //Student Table *      Public classStudent $     {Panax Notoginseng          Public intStudentID {Get;Set; } -          Public stringUserName {Get;Set; } the          Public stringUserpass {Get;Set; } +          Public stringRealname {Get;Set; } A          PublicNullable<system.datetime> Birthday {Get;Set; } the          Publicnullable<int> Classesid {Get;Set; } +          Public VirtualClasses Classes {Get;Set; } -     } $}

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <connectionStrings>    <Addname= "Entity"connectionString= "server=."; database=efdemo;uid=sa;pwd=sa2012 "ProviderName= "System.Data.SqlClient" />  </connectionStrings></Configuration>

            using (varnew  entities ())            {                new  Classes ()                {                    " take 4 classes "                };                Db. Classes.add (CL);                Db. SaveChanges ();            }

====================================================

============================================================

===================================================

==============================================================

================================================================

EF Learning Note 01----------Creating a database using Code First mode

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.