從 .NET Framework 第 4 版開始,Entity Framework 支援數據定義語言 (DDL)。 這可讓您根據連接字串和記憶體 (SSDL) 模型的元數據來建立或刪除資料庫實例。
上的 ObjectContext 下列方法會使用連接字串和 SSDL 內容來完成下列作業:建立或刪除資料庫、檢查資料庫是否存在,以及檢視產生的 DDL 腳本:
備註
執行 DDL 命令會假設有足夠的許可權。
先前列出的方法會將大部分的工作委派給基礎 ADO.NET 數據提供者。 提供者有責任確保用來產生資料庫物件的命名慣例與用於查詢和更新的慣例一致。
下列範例示範如何根據現有的模型產生資料庫。 它也會將新的實體物件新增至物件內容,然後將它儲存至資料庫。
程序
根據現有的模型定義資料庫
建立主控台應用程式。
將現有的模型新增至您的應用程式。
- 新增一個名為
SchoolModel的空模型。 若要建立空的模型,請參閱 如何:建立新的 .edmx 檔案 主題。
SchoolModel.edmx 檔案會新增至您的專案。
從 學校模型 主題複製學校模型的概念、儲存和對應內容。
開啟 SchoolModel.edmx 檔案,並在標記中貼上
edmx:Runtime內容。
- 新增一個名為
將下列程式碼新增至您的 main 函數。 此程式代碼會將連接字串初始化至資料庫伺服器、檢視 DDL 腳本、建立資料庫、將新的實體新增至內容,並將變更儲存至資料庫。
// Initialize the connection string. String connectionString = "..."; using (SchoolEntities context = new SchoolEntities(connectionString)) { try { if (context.DatabaseExists()) { // Make sure the database instance is closed. context.DeleteDatabase(); } // View the database creation script. Console.WriteLine(context.CreateDatabaseScript()); // Create the new database instance based on the storage (SSDL) section // of the .edmx file. context.CreateDatabase(); // The following code adds a new objects to the context // and saves the changes to the database. Department dpt = new Department { Name = "Engineering", Budget = 350000.00M, StartDate = DateTime.Now }; context.Departments.AddObject(dpt); // An entity has a temporary key // until it is saved to the database. Console.WriteLine(dpt.EntityKey.IsTemporary); context.SaveChanges(); // The object was saved and the key // is not temporary any more. Console.WriteLine(dpt.EntityKey.IsTemporary); } catch (InvalidOperationException ex) { Console.WriteLine(ex.InnerException.Message); } catch (NotSupportedException ex) { Console.WriteLine(ex.InnerException.Message); } }' Initialize the connection string. Dim connectionString As String = "metadata=res://*/School.csdl|res://*/School.ssdl|res://*/School.msl;provider=System.Data.SqlClient;" & "provider connection string=""Data Source=.;Initial Catalog=School;Integrated Security=True;MultipleActiveResultSets=True""" Using context As New SchoolEntities(connectionString) Try If context.DatabaseExists() Then ' Make sure the database instance is closed. context.DeleteDatabase() End If ' View the database creation script. Console.WriteLine(context.CreateDatabaseScript()) ' Create the new database instance based on the storage (SSDL) section ' of the .edmx file. context.CreateDatabase() ' The following code adds a new objects to the context ' and saves the changes to the database. Dim dpt As New Department() context.Departments.AddObject(dpt) ' An entity has a temporary key ' until it is saved to the database. Console.WriteLine(dpt.EntityKey.IsTemporary) context.SaveChanges() ' The object was saved and the key ' is not temporary any more. Console.WriteLine(dpt.EntityKey.IsTemporary) Catch ex As InvalidOperationException Console.WriteLine(ex.InnerException.Message) Catch ex As NotSupportedException Console.WriteLine(ex.InnerException.Message) End Try End Using