2011-09-26 169 views
3

我不得不突然转向使用Code First Entity Framework 4.1。我开始不了解这个框架,但在过去的8个小时里,我现在阅读博客和文章更加舒适。如何从Entity Framework中的.edmx文件生成数据库?

This blog in particular是迄今为止我所见过的最好的博客之一,但所给出的步骤与我的经验不符。特别是,我需要更多关注第3和第4步(分别为“创建模型”和“交换到DbContext代码生成”)。我无法从我定义的EntitySet生成数据库。我得到的SQL,我可以执行,但我发现了以下错误:

Could not locate entry in sysdatabases for "MyBD" database . No entry found with that name. Make sure that the name is entered correctly entity framework. 

如果我再次执行SQL,我得到以下已经在数据库中存在的表的名称相同的错误。

如果刷新服务器资源管理器中的DataConnection,则不会创建像我在实体框架中定义的那样的表。

如何摆脱这个错误,并成功地在我的.edmx中生成表格?

此外,我无法在解决方案资源管理器中右键单击选项,以便从具有从DBContext对象继承的上下文类的选定类文件中“生成数据库”。我从Microsoft安装了Entity Framework 4.1,所以它应该出现在那里......我如何获得生成数据库选项?

回答

4

如果从模型创建数据库,则需要先选择空模型。以下是其他步骤来创建数据库:

  1. 选择新的连接
  2. 设置服务器名:如果你安装它,只需键入。选择默认。您也可以尝试(本地)
  3. 设置新的数据库名称
  4. 复制DDL脚本添加到SQL Server Management Studio中的查询屏幕
  5. 运行脚本来创建数据库

脚本运行之后,你将有最初的表格。配置文件将具有名为容器名称的连接字符串。

现在,当您想切换到类似于TT文件示例的代码生成时,您可以右键单击并添加代码生成。它将为实体模型创建部分类,为dbcontext创建一个文件。类似于:

using System; 
    using System.Collections.Generic; 

    public partial class Contact 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 

上下文将只有一个表。

public partial class PersonModelContainer : DbContext 
    { 
     public PersonModelContainer() 
      : base("name=PersonModelContainer") 
     { 
     } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      throw new UnintentionalCodeFirstException(); 
     } 

     public DbSet<Contact> Contacts { get; set; } 
    } 

你不需要TT模型。您可以直接添加这些文件。您需要一个从DbContext继承的上下文类和一个用于每种类型的实体的部分类文件。如果您对模型进行了更改,EF将检测到这一点。您需要定义Db初始值设定项。对于该网页上的示例演示,您可以将初始化程序添加到其他方法。如果它是一个Web项目,那么将这个init函数添加到Global.asax-> application_Start以进行初始开发。初始化程序有不同的选项。我使用drop并为最初的开发创建。

static void InitDbCheck() 
     { 
      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<PersonModelContainer>()); 
      using (var db = new PersonModelContainer()) 
      { 
       //accessing a record will trigger to check db. 
       int recordCount = db.Contacts.Count(); 
      } 
     } 

     static void Main(string[] args) 
     { 



      using (var db = new PersonModelContainer()) 
      { 
       // Save some data 
       db.Contacts.Add(new Contact { Name = "Bob" }); 
       db.Contacts.Add(new Contact { Name = "Ted" }); 
       db.Contacts.Add(new Contact { Name = "Jane" }); 
       db.SaveChanges(); 

       // Use LINQ to access data 
       var people = from p in db.Contacts 
          orderby p.Name 
          select p; 

       Console.WriteLine("All People:"); 
       foreach (var person in people) 
       { 
        Console.WriteLine("- {0}", person.Name); 
       } 

       // Change someones name 
       db.Contacts.First().Name = "Janet"; 
       db.SaveChanges(); 
      } 
     } 
13

下面是从MSDN明确的指导上

How to: Generate a Database from a Conceptual Model (Entity Data Model Tools) [的.edmx]文件。

复制/粘贴在这里只是为了完整起见:

To generate a database from a conceptual model

1 - Add an .edmx file to your project.

For information about adding an .edmx file to a project, see How to: Create a New .edmx File (Entity Data Model Tools) and How to: Add an Existing .edmx File (Entity Data Model Tools).

2 - Build the conceptual model.

You can use the ADO.NET Entity Data Model Designer (Entity Designer) to create entities and relationships or you can manually edit the .edmx file to build a conceptual model. For more information, see Implementing Advanced Entity Framework Features and CSDL, SSDL, and MSL Specifications.

NoteNote When you build the conceptual model, warnings about unmapped entities and associations may appear in the Error List. You can ignore these warnings because the Create Database Wizard will add the storage model and mapping information (see step 3).

3 - Right-click an empty space on the Entity Designer surface and select Generate Database from Model.

The Choose Your Data Connection Dialog Box of the Generate Database Wizard (Entity Data Model Tools) is displayed.

4 - Click the New Connection button or select an existing connection button from the drop-down list to provide a database connection.

You must supply a database connection so that column types for the target database can be determined based on property types in your model, and so that connection string information can be added to your application. Note that supplying connection information does not initiate data definition language (DDL) generation.

5 - Click Next.

The Create Database Wizard generates data definition language for creating a database. The generated DDL is displayed in the Summary and Settings Dialog Box (Generate Database Wizard).

6 - Click Finish.

Upon completion, the Create Database Wizard does the following:

Generates the store schema definition language (SSDL) and mapping specification language (MSL) that correspond to the provided conceptual schema definition language (CSDL). The .edmx file is updated with the generated SSDL and MSL. Note that the wizard overwrites existing SSDL and MSL.

Saves the generated DDL in the location specified in the Save DDL As text box. For more information about the generated DDL, see Database Generation Rules (Generate Database Wizard).

NoteNote If a storage model is already defined when you run the Create Database Wizard, the generated DDL will contain a DROP TABLE statement and DROP CONSTRAINT statement for each EntitySet and each AssociationSet (respectively) that are inferred from the storage model.

Adds connection string information to your App.config or Web.config file.

It is important to note that the Create Database Wizard does not execute the generated DDL. To create the database schema that corresponds to your conceptual model, you must execute the generated DDL independently (for example, execute the DDL in SQL Server Management Studio).

相关问题