Using Entity Framework with DotNetNuke 5

Using the Brandon Haynes DotNetNuke Entity Framework Model Adapter

Also see:

Note: Requires Visual Studio 2008 (or higher), ASP.net 3.5 (or higher), and DotNetNuke 4 (or higher)

Create The Module

Use Visual Studio to open DotNetNuke

Right-click on the App_Code folder and select New Folder

Name the folder EFNotes

Right-click on the DesktopModules folder and select New Folder

Name the folder EFNotes

Open the web.config file and add an entry to the codeSubDirectories section for EFNotes.

Log into your DotNetNuke site as the Host account, and from the Host menu, select SQL

Paste the following script in the box, check, Run as Script and click the Execute link:

CREATE TABLE [dbo].[EF_Notes](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NULL,
    [Message] [nvarchar](500) NULL,
 CONSTRAINT [PK_EF_Notes] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

In Visual Studio, right-click on the EFNotes folder in the App_Code folder and select Add New Item...

Select ADO.NET Entity Data Model

Select Generate from database

Select SiteSqlServer for the connection

Select the EFNotes table and click the Finish button 

The data model will be created. Save and close the file.

Right-click on the EFNotes folder under the DesktopModules folder and select Add New Item...

Create a Web User Control and name it View.ascx. Ensure that Place code in separate file is checked.

Replace all the code in View.ascx with the following code:

<%@ Control Language="C#" 
AutoEventWireup="true" CodeFile="View.ascx.cs" 
Inherits="DotNetNuke.Modules.EFNotes.View" 
%>

Replace all the code in View.ascx.cs with the following code:

using System;
using DotNetNuke.Entities.Modules;

namespace DotNetNuke.Modules.EFNotes
{
    public partial class View : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

On the View.ascx page, switch to Design view.

From the Visual Studio Toolbox drag an EntityDataSource control and drop it on the View.ascx page.

Select Configure Data Source...

Select the Entity you created previously (note the name will probably be different than the name in the picture above).

Select EF_Notes for EntitySetName and check:

Drag and drop a ListView control on the View.ascx page and select the EntityDataSource control as the Data Source.

After it is selected, click the Configure ListView... link

Check:

Switch to Source view for the View.ascx page and in each template section, remove the column for the ID field.

Save and close the page.

 

Configure the DotNetNuke Entity Framework Model Adapter

In order to allow DotNetNuke modules using the Entity Framework to automatically apply a runtime database owner and object qualifier, you will need to use the DotNetNuke Entity Framework Model Adapter.

Download the DotNetNuke Entity Framework Model Adapter from:

http://dnnentityframework.codeplex.com/

Put;

from the package into the bin directory of your DotNetNuke website

Open Model.Designer.cs

Replace:

global::System.Data.Objects.ObjectContext

with

BrandonHaynes.ModelAdapter.EntityFramework.DotNetNukeObjectContext

Save and close the file

Configure the Module

Log into the DotNetNuke site as the Host account and select Module Definitions from the Host menu

Select Create New Module

Enter EFNotes for Name and Friendly Name. Set the Version to 01 00 00 and click Next

Click Next

Click Next

you will be back on the Module Definitions page. Click the edit icon next to the EFNotes entry

Select Add Definition

Enter EFNotes for the Friendly Name and select Create Definition

Select Add Module Control

Enter EFNotes for the Title and select the View.ascx control for the Source. Ensure View is selected for the Type and click Update

Navigate to a blank page in the DotNetNuke site and add the module to the page

The module will now work

Packaging The Module

When you package the module (select the EFNotes module on the Module Configuration page in the DotNetNuke website)

Make sure you include:

[Back to: The ADefWebserver DotNetNuke HELP WebSite]


DotNetNuke® is a registered trademark of the DotNetNuke Corporation