Not logged in : Login
(Sponging disallowed)

About: http://blogs.msdn.com/b/adonet/rss.aspx?Tags=edm/visual+studio+2010/code+first#23     Goto   Sponge   NotDistinct   Permalink

An Entity of Type : sioc:Thread, within Data Space : www.openlinksw.com associated with source document(s)
QRcode icon
http://www.openlinksw.com/describe/?url=http%3A%2F%2Fblogs.msdn.com%2Fb%2Fadonet%2Frss.aspx%3FTags%3Dedm%2Fvisual%2Bstudio%2B2010%2Fcode%2Bfirst%2323

AttributesValues
has container
Date Created
maker
seeAlso
link
Description
  •   The information in this post is out of date. Visit msdn.com/data/ef for the latest information on current and past releases of EF. For Model First se http://msdn.com/data/jj20542 For Database First se http://msdn.com/data/jj20687   This post will provide an introduction to Model First and Database First development with the DbContext API, using the Entity Data Model Designer in Visual Studio. You will need to have Visual Studio 2010 or Visual Studio 11 installed to complete this walkthrough.   1. Create the Application To keep things simple we’re going to build up a basic console application that uses the DbContext to perform data access: Open Visual Studio File -> New -> Project… Select “Windows” from the left menu and “Console Application” Enter “ModelFirstSample” as the name Select “OK”   2. Create the Model Let’s go ahead and add an Entity Data Model to our project; Project –> Add New Item… Select ‘Data’ from the left menu Select ‘ADO.NET Entity Data Model’ from the list of available items Name the model ‘PersonModel.edmx’ Click ‘Add’ We are going to use Model First for this walkthrough but if you are mapping to an existing database you would now select ‘Generate from database’, follow the prompts and then skip to step 4. Select ‘Empty model’ Click ‘Finish’ Let’s add a Person entity to our model: On the design surface; Right Click –> Add –> Entity Name the entity ‘Person’ Click ‘OK’ On the Person entity; Right Click –> Add –> Scalar Property Name the property ‘Full Name’   3. Create the Database Now that we’ve defined the model we can generate a database schema to store our data: On the design surface; Right Click –> Generate Database from Model Click ‘New Connection…’ Specify the details of the database you wish to create Click ‘OK’ If prompted to create the database; click ‘Yes’ Click ‘Next’ then ‘Finish’ On the generated script; Right Click –> Execute SQL… Specify your database server and click ‘Connect’   4. Swap to DbContext Code Generation The PersonModel is currently generating a derived ObjectContext and entity classes that derive from EntityObject, we want to make use of the simplified DbContext API. To use DbContext we need to install the EntityFramework NuGet package: Project –> Add Library Package Reference… Select the “Online” tab Select the “EntityFramework” package Click “Install” Now we can swap to using DbContext code generation templates: On the design surface; Right Click –> Add Code Generation Item… Select ‘Online Templates’ from the left menu Search for ‘DbContext’ Select ‘EF 4.x DbContext Generator’ from the list (If you are using EF 5 you should select ‘EF 5.x DbContext Generator’ instead) Name the item ‘PersonModel.tt’ Click ‘Add’ You’ll notice that two items are added to your project: PersonModel.tt This template generates very simple POCO classes for each entity in your model PersonModel.Context.tt This template generates a derived DbContext to use for querying and persisting data   5. Read & Write Data It’s time to access some data, I’m padding out the Main method in Program.cs file as follows; class Program { static void Main(string[] args) { using (var db = new PersonModelContainer()) { // Save some data db.People.Add(new Person { FullName = "Bob" }); db.People.Add(new Person { FullName = "Ted" }); db.People.Add(new Person { FullName = "Jane" }); db.SaveChanges(); // Use LINQ to access data var people = from p in db.People orderby p.FullName select p; Console.WriteLine("All People:"); foreach (var person in people) { Console.WriteLine("- {0}", person.FullName); } // Change someones name db.People.First().FullName = "Janet"; db.SaveChanges(); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }   Summary In this walkthrough we looked at Model First and Database First development using the DbContext API. We looked at building a model, generating a database, swapping to DbContext code generation and then saving and querying data. Rowan Miller Program Manager ADO.NET Entity Framework
Link
Title
  • EF 4.2 Model & Database First Walkthrough
topic
type
is made of
is container of of
Faceted Search & Find service v1.17_git122 as of Jan 03 2023


Alternative Linked Data Documents: iSPARQL | ODE     Content Formats:   [cxml] [csv]     RDF   [text] [turtle] [ld+json] [rdf+json] [rdf+xml]     ODATA   [atom+xml] [odata+json]     Microdata   [microdata+json] [html]    About   
This material is Open Knowledge   W3C Semantic Web Technology [RDF Data] Valid XHTML + RDFa
OpenLink Virtuoso version 08.03.3330 as of Apr 5 2024, on Linux (x86_64-generic-linux-glibc25), Single-Server Edition (30 GB total memory, 26 GB memory in use)
Data on this page belongs to its respective rights holders.
Virtuoso Faceted Browser Copyright © 2009-2024 OpenLink Software