8.13. Model-First Generation

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.

  1. Create a new C# console project called Chapter8.ModelFirst.

  2. Add a new ADO.NET Entity Data Model called CustomerModel.

  3. Click Next.

  4. Select "Empty model" (Figure 8-14) on the next step and click Finish.

    Figure 8.14. Select the "Empty model" option
  5. Open the newly created empty CustomerModel.edmx.

  6. Right-click the design surface and select Add Entity.

  7. Call the entity Customer.

  8. Change the key property name to CustomerID (Figure 8-15).

  9. Right-click Customer and select Add Scalar Property. Call it Firstname.

  10. Add three more properties: Lastname, Company, and Phone.

  11. Add another entity called Address.

  12. Change the key property name to AddressID.

  13. Add five scalar properties to Address called Address1, Address2, Address3, City, and PostalCode (Figure 8-16).

    Figure 8.15. Adding an entity to your blank model

    Figure 8.16. Your manually created Customer and Address entities
  14. You need to give Visual Studio a bit more information about the fields for this entity; otherwise, when it creates the database structure, all fields will be created in the format varchar(max). Select the Firstname field; then in the Properties window set the MaxLength property to 100.

  15. Repeat this for the other fields (Figure 8-17).

    Figure 8.17. Setting field length properties
  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.

    Figure 8.18. Adding an association
  17. Accept the association defaults and then click OK.

  18. Select the Model Browser tab next to the Solution Explorer tab.

  19. Right-click the CustomerModel node and select Generate Database from Model (Figure 8-19).

    Figure 8.19. Generating database schema from the entity model
  20. The Choose Your Data Connection dialog will now pop up.

  21. 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).

    Figure 8.20. Generated T-SQL for our EDM

    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

  22. Click Finish.

  23. You will receive a warning (Figure 8-21)—click Yes.

    Figure 8.21. Warning displayed on generated T-SQL

That's it—you can now run this SQL on your database and use the EDM in the standard way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.68.177