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