.Net / Data Analysis / Interactive Web Data

Incorporating the EntityFramework & ADO.NET Entity Data Model

The type or namespace name <model_name> could not be found (are you missing a using directive or an assembly reference?)


My current project, the Interactive Data Analysis project, is based on a dynamic webpage. The web page will be linked to a SQL Server database via the Entity Framework and a ADO.NET Entity Data Model. The Entity Framework was chosen purely and simply for its ease-of-use. However, getting it working required a fair amount of experimentation. This series of blogs will document the errors I encountered along the way and attempt to identify the reasons for these errors and the solutions used. In a future post, I will discuss the Entity Framework in more detail, its uses, limitations and alternatives.

I have setup an ExampleProject in which to experiment with. The ExampleProject includes a simple web form and a supporting database. The web form is shown in Figure 1, the data model is shown in Figure 2.

Figure 1: ExampleProject - Web Form

Figure 1: ExampleProject – Web Form

Figure 2: ExampleProject - Data Model

Figure 2: ExampleProject – Data Model

 

 

 

 

 

 

 

 

 

 

 

 

The web form allows the following:

  • accepts a user’s name
  • updates the ID field from the database (by querying the [person] entity)
  • OnSubmit, updates the [birthday] entity with the SelectedDate from the calendar shown

An ADO.NET Entity Data Model has been added to the project and provides the interface between the project and the database. The submit button has the following event handler:


protected void submit_click(object sender, EventArgs e)
{
    String lcName = txt_userInput.Text;
    int validNameID = 0;

    validNameID = exampleBLL.validateName(lcName);

    txt_UserID.Text = validNameID.ToString();

    DateTime lcBirthDate = Calendar1.SelectedDate;
    exampleBLL.loadBirthDate(validNameID, lcBirthDate);
 }

The call to the database is handled in the Business Logic Layer, exampleBLL. The validateName and loadBirthdate methods are given below:

public static int validateName(String aName)
{
    int isValid = 0;
    int validID = -1;

    using (var dbcontext = new exampleEntities())
    {
        var isName = dbContext.people.Where(i => (i.peopleName == aName))
                                     .Select(i => i)
                                     .FirstOrDefault();

        isValid = (isName != null) ? 1 : 0;

        if (isValid == 1)
        {
            validID = isName.peopleID;
            return validID;
        }
    }
    return validID;
}

public static void loadBirthDate(int lclPerson, DateTime lclBirthDate)
{
    using (var dbContext = new exampleEntities())
    {
        var newBirthday = new birthday();
        newBirthday.peopleID = lclPerson;
        newBirthday.birthDate = lclBirthDate;

        dbContext.birthdays.AddObject(newBirthday);
        dbContext.SaveChanges();
    }
}

Trusting in the ease of Visual Studio, I blindly “followed my nose” to include the ADO.NET Entity Data Model into the project. However, when I first tried to debug this program I got the following error:

type_or_namespace_could_not_be_found

The type or namespace name <model_name> could not be found (are you missing a using directive or an assembly reference?)

After a bit of time on Google and some trial and error, I identified the problem… the entity and context classes were missing from the exampleModel.Designer.cs code file (see the above image).

I attempted the following experiments to correct this:

Experiment 1 – “Add code Generation Item”
Open the .edmx model, right click anywhere in the model and select “Add Code Generation Item”. This successfully populated the Entity and Context methods in the exampleModel.Deisgner.cs code file, however it also created two additional files, exampleModel.tt and exampleModel.Context.tt which duplicate the Context and Entity methods under a different context (or namespace). This gave rise to a further error:

edmEntityTypeAttributeThe type <type_name> is not attributed with EdmEntityTypeAttribute but is contained in an assembly attributed with EdmSchemaSttribute. POCO entities that do not use EdmEntityTypeAttribute cannot be contained in the same assembly as non-POCO entities that use EdmEntityTypeAttirubute.

Experiment 2 – Removing the exampleModel.tt and exampleModel.Context.tt files
After a lot more Goolging, I realised that the methods within the exampleModel.Designer.cs file were conflicting with the methods created by the Code Generation Item (the code in the Context.tt and .tt files). The following steps fixed this:

  1. delete the exampleModel.tt and exampleModel.Context.tt files
  2. Open the .edmx diagram, open the properties and change the “Code Generation Strategy” from “T4” to “Legacy ObjectContext”.
  3. Finally, save the .edmx diagram (ctrl-s) and this will automatically populate the exampleModel.Designer.cs methods for you.

That’s it, it is that easy!

In Summary
It is “Visual Studio – easy” (point and click) to add the ADO.NET Entity Data Model to a project. Follow the wizard, and when the .edmx diagram opens up, change the “Code Generation Strategy” to “Legacy ObjectContext” and then save the .edmx diagram. All done, the code now works a treat.

Future Questions:

What is the difference between the “Legacy ObjectContext” and the “T4” options? I don’t know, but will find out in the near future.

Why would you choose to use the Context.tt and .tt code files instead of the Designer.cs files? I don’t know, but will also hunt out an answer to this.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s