EF 4.1 Model & Database First Walkthrough 学习摘要

一个非常简单方便的 Entity Framework 初体验教程之二。

原文from: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx


1. Install EF 4.1

2. Create the Application

3. 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’

PersonModel.edmx

    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:

  • On the design surface; Right Click –> Add Code Generation Item…
  • Select ‘Code’ from the left menu
  • Select ‘ADO.NET DbContext Generator
  • 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

    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();
    }
}

转载于:https://www.cnblogs.com/lovezming/archive/2012/11/28/2793818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值