This article aims to have a simple perceptual knowledge of EntityFramework's code first mode through a small exercise.
Environment: Vs2013,sql Server 2012
Open SQL Server Management Studio.
Object Explorer | database | right | New database, enter database name "bookshop", OK.
Object Explorer | database | bookshop| new query, enter in the new window that opens:
CREATE TABLEBooks ([ID] INT not NULL PRIMARY KEY IDENTITY, [Title] NVARCHAR( -) not NULL, [Author] NVARCHAR( -) not NULL, [ Price] DECIMAL( -,2) not NULL)INSERT intoBooks ([Title],[Author],[ Price])VALUES('Getting Started with WinForm programming','Zhang San',34.50)INSERT intoBooks ([Title],[Author],[ Price])VALUES('Getting started with ASP. NET MVC programming','John Doe',32.10)INSERT intoBooks ([Title],[Author],[ Price])VALUES('The governance of the capital','Sima Guang',88.50)INSERT intoBooks ([Title],[Author],[ Price])VALUES('Historical Records','Sima Qian',132.10)INSERT intoBooks ([Title],[Author],[ Price])VALUES('Journey to the','Wu Chengen',28.50)INSERT intoBooks ([Title],[Author],[ Price])VALUES('Red Mansions','Cao Xueqin',132.10)
Click Execute in the toolbar, the Books table and the initial data are set up.
Open VS2013 and create a new console application.
Project | right | manage NuGet package, in open dialog box, left select online, the search in upper right corner enter "EntityFramework", find and click Install, after completion will be in the solution directory more than a "packages" directory.
To add a connection string in app. config:
< connectionStrings > < name= "Dbbookcontext" connectionString= "Data source=.\sqlexpress;initial Catalog=bookshop; User Id=sa; password=*** " providerName=" System.Data.SqlClient "/> </ connectionStrings>
Add the corresponding code in the Program.cs:
Public classBook { Public intID {Get;Set; } Public stringTitle {Get;Set; } Public stringAuthor {Get;Set; } Public decimalPrice {Get;Set; } } Public classDbbookcontext:dbcontext {//Public Dbbookcontext ()//{ //Database.Connection.ConnectionString = @ "Data source=.\sqlexpress;initial catalog=bookshop; User Id=sa; Password=server01 "; //} PublicDbset<book> Books {Get;Set; } } classProgram {Static voidMain (string[] args) {Dbbookcontext Bookcontext=NewDbbookcontext (); IQueryable<Book> books =Bookcontext.books; foreach(Book BookinchBooks. (b=>B.price)) {Console.WriteLine (String.Format ("{0},{1},{2},{3}", Book.id, book. Title, book. Author, book. Price)); } } }
build and run. ( a connection string can also be assigned to a Dbbookcontext constructor.) )
Test one: Change the Dbbookcontext to another name, can not find the connection string, run without error, there is no result, the database generated a new database.
Experiment Two: Rename the ID in book, report the exception: EntityType ' book ' has no key defined.
Experiment Three: Change book to Book1, run no error, run after the database more than two empty table: Book1,__migrationhistory, at this time again to change the code back, run will report abnormal, in the database delete __migrationhistory just fine.
Experiment Four: Change the books field of Dbbookcontext to another name, the operation result is normal, this name casually up.
Preliminary conclusion, the database table name is the plural form of the class name, try foot, the generated table name is feet, if it is not the normal word, directly add an "s".
C # entityframework Simple Getting Started