Menu
Menu
Follow Us
Blog
 
Printable Version Printable Version Recommend Recommend Email to a friend Email to a friend

Forms and Questionnaire Module for DNN (DotNetNuke) - Part 3

A tutorial on how to create a DNN module: Entity Framework
07/18/2012

Entity Framework in DotNetNuke Module Development

I am getting closer to the developing stage of the Forms and questionnaire DNN module and I was wondering if I could use the Entity Framework for my data access layer. Potentially it could save me some time and effort related to creating and maintaining a data access layer.

As I was reading on the subject I found out that there is an issue with DotNetNuke’s database owner and object qualifier as the Entity Framework does not allow for an easy way to add the object qualifier and database owner at runtime.  Eventually I found out that there is an adapter that can do the job (written by Brandon Haynes).

Unfortunately, as I later discovered, this adapter only works with .Net Framework 3.5. If I compiled my module for DotNetNuke with .Net Framework 4.0 I would get a server error. I have not looked into the error much but my thoughts were that the adapter was written before the release of Entity Framework 4 so there might be a change made to the entity data model that no longer allows for the adapter to work properly. There is a mention here that a new adapter is being worked on but the source has not been posted yet.

In any case here is the procedure I followed to create a DotNetNuke module shell with .Net Framework 3.5. It is just a shell and not meant to do anything. The only purpose is to prove that we can make use of the entity framework with the adapter.  I used Visual Studio 2010 to create the project.

  1. Create a new class library project named "DNNEntityFramework" with .Net Framework 3.5. The entity data model will be compiled with this project.
  2. Delete Class1.
  3. Add New item - ADO.NET Entity Data Model – name it “DNNModel.edmx”
  4. Select Generate from database.
  5. For connection string you need to have a SQL server running with a DotNetNuke database already installed. Connect to that database. The database should not have an object qualifier.
  6. Click Save entity connection settings in AppConfig checkbox and change the name to DnnEntities:
  7. Set the Model namespace to “DnnModel”.
  8. Expand tables and choose Tabs, TabModules, and Modules.
  9. Click Finish.
  10. Add references to BrandonHaynes.ModelAdapter.EntityFramework.dll and BrandonHaynes.ModelAdapter.EntityFramework.DotNetNuke.dll These can be downloaded here.

  11. Edit DNNModel.edmx.cs and replace the

    public partialclass DnnEntities: global::System.Data.Objects.ObjectContext

    with

    public partial class DnnEntities :BrandonHaynes.ModelAdapter.EntityFramework.DotNetNukeObjectContext

  12. Right click solution and add a new project. Choose ASP.NET Web Application. This application should be set with .Net Framework 3.5 too. This project will be our compiled DNN module. 
  13. Name it “DNNEntityFrameworkModule”.
  14. Delete Auto generated files.
  15. Right click and add new item. Add a web user control.
  16. Add a reference to
    a
    .      DotNetNuke.dll
    b.     DNNEntityFramework Project,
    c.      System.Data.Entity (3.5)
    d.     BrandonHaynes.ModelAdapter.EntityFramework.dll
    e.     BrandonHaynes.ModelAdapter.EntityFramework.DotNetNuke.dll
  1. 17  Edit ModuleView.ascx.cs

    18.      Change

    public partialclass ModuleView: System.Web.UI.UserControl

    to

                    public partialclass ModuleView: DotNetNuke.Entities.Modules.PortalModuleBase

     

    19.      Add a label to the ModuleView.ascx file

    <asp:Label id="lblMessage"runat="server"></asp:Label>

    20.      Paste the following code Page_Load event of theModuleView.ascx

protected void Page_Load(object sender, EventArgse)

        {

            string connectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;

           DNNEntityFramework.DnnEntitiescontext = new DNNEntityFramework.DnnEntities("metadata=res://*/DNNModel.csdl|res://*/DNNModel.ssdl|res://*/DNNModel.msl;provider= System.Data.SqlClient;providerconnection string=\"" + connectionString + "MultipleActiveResultSets=True\"");

            var tabs = from c in context.Tabs

                       selectc;

            foreach (DNNEntityFramework.Tabtab in tabs)

            {

               lblMessage.Text += tab.TabID + " "+ tab.TabName + "<br />";

            }

        }

Here I have a workaround for reading in the entitydata model connection string. I just took the full connection string from the“DnnEntityFramework” project’s AppConfig file and placed it in my code,replacing the database connection with the one from the web.config. In realprojects this should probably be stored in the web.config upon moduleinstallation. 

21. Compile the project

After compiling we are ready to test the module in a DotNetNuke installation. What I did is I created a DNN module manually. Just copy the four resulting dll files from our solution to the bin of a DotNetNuke installation.  Then copy the “ModuleView.ascx” file under /DesktopModules/DnnEntityFramework/. Log in to the portal and create a new module manually by using Host->Extension from the newly created “/DesktopModules/DnnEntityFramework/ ModuleView.ascx” file.

Place the DotNetNuke module on a page and the result should look likethis:

Here is the error I get if I compile with .Net Framework 4.0

Exception Details: System.Data.MappingException: Schema specified is not valid. Errors: 
(0,0) : error 2007: The Table 'test1_Modules'specified as part of this MSL does not exist in MetadataWorkspace.
(0,0) : error 2063: At least one property mustbe mapped in the set mapping for 'Modules'.
(0,0) : error 2007: The Table 'test1_TabModules'specified as part of this MSL does not exist in MetadataWorkspace.
(0,0) : error 2063: At least one property mustbe mapped in the set mapping for 'TabModules'.
(0,0) : error 2007: The Table 'test1_Tabs'specified as part of this MSL does not exist in MetadataWorkspace.
(0,0) : error 2063: At least one property mustbe mapped in the set mapping for 'Tabs'.
(0,0) : error 2007: The Table 'test1_TabModules'specified as part of this MSL does not exist in MetadataWorkspace.
(0,0) : error 2007: The Table 'test1_TabModules'specified as part of this MSL does not exist in MetadataWorkspace.
(0,0) : error 2007: The Table 'test1_Tabs'specified as part of this MSL does not exist in MetadataWorkspace.

The DotNetNuke website that I was using for this test had an objectQualifier=”test1”. Remember that when you generate the model you should use a database with no objectQualifier. 

Comments (0)

 
PackFlash| Superior DotNetNuke Solutions| 415 N. Lasalle Street, Suite 205, Chicago, IL 60654| success@packflash.com
Home | DotNetNuke Modules | Skins & Design | Website Services | Demos | About Us | Contact Us | Blog
Copyright 2019 by PackFlash