Blog from Saravanan Arumugam

Let us talk about Technologies

Tag Archives: Entity framework

Data formatting in HtmlHelper.EditFor


I have a date time filed coming from entity model. When I use @Html.DisplayFor() or @Html.EditFor(), I am getting a long date-time string like “2012-04-05 11:00:40.270”. I wanted to display just the date not the time.

I tried using @Html.EditorFor(model => ((DateTime)model.ShiftDate).ToShortDateString()). It threw me the following runtime exception.

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Eventually I figured out that System.ComponentModel.DataAnnotations.DisplayFormatAttribute can help to format the date. If the model is a class you created in your Model folder, we can simply apply the DisplayForatAttribute and get the result.

But to format the Entity’s property, or a service’s DataContract member, we wouldn’t want to apply the attribute directly, since every Service update would refresh what we coded.

The best thing to do is to extend the Entity or DataContract by writing a partial class and add an additional property. Then add the formatting to the property.

Here is what I did to display the formatted “Shift Date” of “Record” class.

public partial class Record
{        
    [DisplayName("Shift Date")]
    [DisplayFormat(DataFormatString="{0:MM/dd/yyyy}", 
        ApplyFormatInEditMode=true)]
    public System.Nullable<System.DateTime> FormattedShiftDate
    {
        get
        {
            return this.ShiftDateField;
        }
        set
        {
            this.ShiftDateField = value;
        }
    }
}

Write a partial class (remember to specify the same namespace as the entity), create an additional Property which internally works with the same original field. In my case, I created FormattedShiftDate, where I used the DisplayNameAttribute to name the property as Shift Date. I have formatted the Property with DisplayFormatAttribute with DataFormatString=”MM/dd/yyyy”.

In the views, instead of using ShiftDate in the helper methods, start using FormattedShiftDate.

@Html.EditorFor(model => model.FormattedShiftDate)
@Html.ValidationMessageFor(model => model.FormattedShiftDate)
Advertisements

The type ‘System.Data.Objects.DataClasses.EntityObject’ is defined in an assembly that is not referenced


I am working on an MVC 3 application where I use an Entity Object as the model to the View. I was getting the following error in the browser.

Exception

Compiler Error Message: CS0012: The type ‘System.Data.Objects.DataClasses.EntityObject’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.

Solution:

It was interesting that the MVC project by default has the reference to System.Data.Entity namespace. But still the issue was presented. I finally figured out the clue from another word press blog. I had to add the assembly entry in the Web.config to resolve the issue.

  <assemblies>

    <add assembly=System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 />

    <add assembly=System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 />

    <add assembly=System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 />

    <add assembly=System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 />

    <add assembly=System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 />

    <add assembly=System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089/>

  </assemblies>

Note that the complete assembly name is presented in the exception message itself.

‘__Entities’: type used in a using statement must be implicitly convertible to ‘System.IDisposable’


I have a solution where my working project references the project having the Entity Data Model. When I create the entity model context with the using statement I get the following compiler error.

Exception

Error 2 ‘Intrado.Data.MyEntities’: type used in a using statement must be implicitly convertible to ‘System.IDisposable’ C:\Saravanan\MyApp\MyApp.Service.cs 16 13 MyApp.Service

Solution

We can find this issue when the entity model is in a different project than the working project. We get this exception since the Compiler knows till the point that the entity class (MyEntities in my example) is derived from ObjectContext. For some reason C# compiler can’t understanding that ObjectContext has implemented the IDisposible.

To resolve it, simply add a reference to the System.Data.Entity in the working project. This should take care of this error.

The specified named connection is either not found in the configuration


I created a entity data model in one project and used it by referencing in a different project. .Net framework didn’t let me do this just by adding a project reference.

Exception

I got the following exception.

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid

Solution

When I searched for a solution to this issue, I got a strange answer as the solution. Even though the project containing the entity model contains the named connection string in its app.config, it was required to copy it to the configuration file of the project referencing the entity. I had to manually copy the connection string from the app.config to the configuration of the web application where I used the entity.