EF4 allows you to create your entity model in Visual Studio and use it to generate and update database structure. At the time of writing, this works only with SQL Server. This facility is great for users unfamiliar with SQL or in situations where you do not have access to the database.
Create a new C# console project called Chapter8.ModelFirst.
Add a new ADO.NET Entity Data Model called CustomerModel.
Click Next.
Select "Empty model" (Figure 8-14) on the next step and click Finish.
Open the newly created empty CustomerModel.edmx.
Right-click the design surface and select Add Entity.
Call the entity Customer.
Change the key property name to CustomerID (Figure 8-15).
Right-click Customer and select Add Scalar Property. Call it Firstname.
Add three more properties: Lastname, Company, and Phone.
Add another entity called Address.
Change the key property name to AddressID.
Add five scalar properties to Address called Address1, Address2, Address3, City, and PostalCode (Figure 8-16).
You now need to link your Customer and Address entities. Right-click the design surface and select the Add Association option. You'll see the screen in Figure 8-18.
Right-click the CustomerModel node and select Generate Database from Model (Figure 8-19).
The Choose Your Data Connection dialog will now pop up.
Select the connection we used earlier, select the "Yes, include the sensitive data in the connection string" option, and click Next. Visual Studio will then generate the necessary SQL to create a structure to hold these entities (Figure 8-20).
The following is an excerpt of some of the T-SQL that will be generated:
-- Creating table 'Customers' CREATE TABLE [dbo].[Customers] ( [CustomerID] int NOT NULL, [Firstname] nvarchar(100) NOT NULL, [Lastname] nvarchar(100) NOT NULL, [Company] nvarchar(100) NOT NULL, [Phone] nvarchar(100) NOT NULL ); GO -- Creating table 'Addresses' CREATE TABLE [dbo].[Addresses] ( [AddressID] int NOT NULL, [Address1] nvarchar(100) NOT NULL, [Address2] nvarchar(100) NOT NULL, [Address3] nvarchar(100) NOT NULL, [City] nvarchar(100) NOT NULL, [PostalCode] nvarchar(100) NOT NULL ); GO
-- -------------------------------------------------- -- Creating all Primary Key Constraints -- -------------------------------------------------- -- Creating primary key on [CustomerID] in table 'Customers' ALTER TABLE [dbo].[Customers] WITH NOCHECK ADD CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED ([CustomerID] ASC) ON [PRIMARY] GO -- Creating primary key on [AddressID] in table 'Addresses' ALTER TABLE [dbo].[Addresses] WITH NOCHECK ADD CONSTRAINT [PK_Addresses] PRIMARY KEY CLUSTERED ([AddressID] ASC) ON [PRIMARY] GO -- -------------------------------------------------- -- Creating all Foreign Key Constraints -- -------------------------------------------------- -- Creating foreign key on [CustomerCustomerID] in table 'Addresses' ALTER TABLE [dbo].[Addresses] WITH NOCHECK ADD CONSTRAINT [FK_CustomerAddress] FOREIGN KEY ([CustomerCustomerID]) REFERENCES [dbo].[Customers] ([CustomerID]) ON DELETE NO ACTION ON UPDATE NO ACTION GO
You will receive a warning (Figure 8-21)—click Yes.
That's it—you can now run this SQL on your database and use the EDM in the standard way.
18.224.68.177