type used in a using statement must be implicity convertible to system.Idisposable

Mick picture Mick · Aug 5, 2014 · Viewed 18.2k times · Source

Started a new mvc4 project. Used the MVC controller template to create the read/write actions of the 'Orders' Model. Added a new Entity framework connection to my db and am attempting to update it with a new order.

[HttpPost]
    public ActionResult Create(Orders orders)
    {
        try
        {

            //type used in a using statement must be implicity convertible to system.Idisposable
            using (BuyABicycleEntities db = new BuyABicycleEntities())
            {
                Order NewOrder = new Order();
                NewOrder.BicycleColour = orders.BicycleColour;
                NewOrder.BicycleModel = orders.BicycleModel;
                NewOrder.BicycleSize = orders.BicycleSize;
                NewOrder.CustomerAddress = orders.CustomerAddress;
                NewOrder.CustomerEmail = orders.CustomerEmail;
                NewOrder.CustomerName = orders.CustomerName;
                NewOrder.CustomerPhoneNumber = orders.CustomerPhoneNumber;

                //entiry framework new item
                //db.Orders.(NewOrder);
                db.SaveChanges();

            }

            return View(orders);
        }
        catch (Exception ex)
        {
            //Show Message("Error in Orders - " + ex.Message);

        }
    }

however the using statement is flagging an error:

type used in a using statement must be implicity convertible to system.Idisposable

So I have to implement an idisposable?, will I have to do this everytime I want to update the db? Searched through stackoverflow alot of similar Q's but still unsure as to how to dispose of mine? please advise. thanks

    // ------------------------------------------------------------------------------
// DO NOT MODIFY THIS CLASS - AutoGenerated Code
//    Any changes made to this code will be lost the next time this 
//    code is regenerated.
// 
//    Generated at: 05/08/2014 14:16:31
//    DevForce version: 6.1.7.0
//    Template version: 2.1.4
// ------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using IbEm   = IdeaBlade.EntityModel;
using IbCore = IdeaBlade.Core;
using IbVal  = IdeaBlade.Validation;

[module: IbCore.IdeaBladeLicense("5w+U/Qneh0KYrGDaCOhKKqJpzM0BhW5i7ky77IZpGawLem2DQ5JuP6W8fhhcpF/x")]

namespace MvcApplication18 { 

  #region BuyABicycleEntities

  /// <summary>
  /// The domain-specific EntityManager for your domain model.
  /// </summary>
  public partial class BuyABicycleEntities : IbEm.EntityManager {

    #region Constructors

    /// <summary>See <see cref="M:IbEm.EntityManager.#ctor(bool, string, IbEm.EntityServiceOption, string)"/>. </summary>
    public BuyABicycleEntities(bool shouldConnect=true, string dataSourceExtension=null, IbEm.EntityServiceOption entityServiceOption=IbEm.EntityServiceOption.UseDefaultService, string compositionContextName=null) : base(shouldConnect, dataSourceExtension, entityServiceOption, compositionContextName) {}

    /// <summary>See <see cref="M:IbEm.EntityManager.#ctor(IbEm.EntityManagerContext)"/>. </summary>
    public BuyABicycleEntities(IbEm.EntityManagerContext entityManagerContext) : base(entityManagerContext) {}

    /// <summary>See <see cref="M:IbEm.EntityManager.#ctor(IbEm.EntityManager, bool, string, IbEm.EntityServiceOption, string)"/>. </summary>
    public BuyABicycleEntities(IbEm.EntityManager entityManager, bool shouldConnect, string dataSourceExtension=null, IbEm.EntityServiceOption entityServiceOption=IbEm.EntityServiceOption.UseDefaultService, string compositionContextName=null) : base(entityManager, shouldConnect, dataSourceExtension, entityServiceOption, compositionContextName) {}

    /// <summary>See <see cref="M:IbEm.EntityManager.#ctor(IbEm.EntityManager, IbEm.EntityManagerContext)"/>. </summary>
    public BuyABicycleEntities(IbEm.EntityManager entityManager, IbEm.EntityManagerContext entityManagerContext=null) : base(entityManager, entityManagerContext) {}

    static BuyABicycleEntities() {
      IbEm.EntityRelation.InitializeEntityRelations(System.Reflection.Assembly.GetExecutingAssembly());
    }
    #endregion Constructors

    #region EntityQueries

    /// <summary>Gets the <see cref="T:IbEm.EntityQuery"/> associated with the given EntitySet name. </summary>
    public IbEm.EntityQuery<BicycleModel> BicycleModels {
      get { return new IbEm.EntityQuery<BicycleModel>("BicycleModels", this); }
    }

    /// <summary>Gets the <see cref="T:IbEm.EntityQuery"/> associated with the given EntitySet name. </summary>
    public IbEm.EntityQuery<Customer> Customers {
      get { return new IbEm.EntityQuery<Customer>("Customers", this); }
    }

    /// <summary>Gets the <see cref="T:IbEm.EntityQuery"/> associated with the given EntitySet name. </summary>
    public IbEm.EntityQuery<Order> Orders {
      get { return new IbEm.EntityQuery<Order>("Orders", this); }
    }
    #endregion EntityQueries
  }
  #endregion BuyABicycleEntities

  #region BicycleModel class

  /// <summary>The auto-generated BicycleModel class. </summary>
  [DataContract(IsReference=true)]
  [IbEm.DataSourceKeyName(@"BuyABicycleEntities")]
  [IbEm.DefaultEntitySetName(@"BuyABicycleEntities.BicycleModels")]
  public partial class BicycleModel : IbEm.Entity {

    /// <summary>Returns the property path for the given expression. </summary>
    /// <example>
    /// Usage:
    /// <code>
    ///    var r = Employee.PathFor(e => e.Manager.City); // returns "Manager.City"
    /// </code>
    /// </example>
    public static string PathFor(System.Linq.Expressions.Expression<System.Func<BicycleModel, object>> expr) {
      return IbCore.PropertyPath.For<BicycleModel>(expr);
    }

    #region Data Properties

    /// <summary>Gets or sets the Id. </summary>
    [Key]
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="Id", AutoGenerateField=true)]
    [IbVal.RequiredValueVerifier( ErrorMessageResourceName="BicycleModel_Id")]
    public int Id {
      get { return PropertyMetadata.Id.GetValue(this); }
      set { PropertyMetadata.Id.SetValue(this, value); }
    }

    /// <summary>Gets or sets the ModelName. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="ModelName", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="BicycleModel_ModelName")]
    public string ModelName {
      get { return PropertyMetadata.ModelName.GetValue(this); }
      set { PropertyMetadata.ModelName.SetValue(this, value); }
    }
    #endregion Data Properties

    #region Navigation properties
    #endregion Navigation properties

    #region EntityProperty definitions
    public partial class PropertyMetadata {

      /// Explicit static constructor ensures static fields are initialized.
      static PropertyMetadata() {}

      /// <summary>The Id <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<BicycleModel, int> Id = new IbEm.DataEntityProperty<BicycleModel, int>("Id", false, true, IbEm.ConcurrencyStrategy.None, true, null);

      /// <summary>The ModelName <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<BicycleModel, string> ModelName = new IbEm.DataEntityProperty<BicycleModel, string>("ModelName", false, false, IbEm.ConcurrencyStrategy.None, false, null);
    }
    #endregion EntityProperty definitions

    #region EntityPropertyNames
    public new partial class EntityPropertyNames : IbEm.Entity.EntityPropertyNames {
      public const String Id = "Id";
      public const String ModelName = "ModelName";
    }
    #endregion EntityPropertyNames
  }
  #endregion BicycleModel class

  #region Customer class

  /// <summary>The auto-generated Customer class. </summary>
  [DataContract(IsReference=true)]
  [IbEm.DataSourceKeyName(@"BuyABicycleEntities")]
  [IbEm.DefaultEntitySetName(@"BuyABicycleEntities.Customers")]
  public partial class Customer : IbEm.Entity {

    /// <summary>Returns the property path for the given expression. </summary>
    /// <example>
    /// Usage:
    /// <code>
    ///    var r = Employee.PathFor(e => e.Manager.City); // returns "Manager.City"
    /// </code>
    /// </example>
    public static string PathFor(System.Linq.Expressions.Expression<System.Func<Customer, object>> expr) {
      return IbCore.PropertyPath.For<Customer>(expr);
    }

    #region Data Properties

    /// <summary>Gets or sets the Id. </summary>
    [Key]
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="Id", AutoGenerateField=true)]
    [IbVal.RequiredValueVerifier( ErrorMessageResourceName="Customer_Id")]
    public int Id {
      get { return PropertyMetadata.Id.GetValue(this); }
      set { PropertyMetadata.Id.SetValue(this, value); }
    }

    /// <summary>Gets or sets the CustomerName. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="CustomerName", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Customer_CustomerName")]
    public string CustomerName {
      get { return PropertyMetadata.CustomerName.GetValue(this); }
      set { PropertyMetadata.CustomerName.SetValue(this, value); }
    }
    #endregion Data Properties

    #region Navigation properties
    #endregion Navigation properties

    #region EntityProperty definitions
    public partial class PropertyMetadata {

      /// Explicit static constructor ensures static fields are initialized.
      static PropertyMetadata() {}

      /// <summary>The Id <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Customer, int> Id = new IbEm.DataEntityProperty<Customer, int>("Id", false, true, IbEm.ConcurrencyStrategy.None, true, null);

      /// <summary>The CustomerName <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Customer, string> CustomerName = new IbEm.DataEntityProperty<Customer, string>("CustomerName", false, false, IbEm.ConcurrencyStrategy.None, false, null);
    }
    #endregion EntityProperty definitions

    #region EntityPropertyNames
    public new partial class EntityPropertyNames : IbEm.Entity.EntityPropertyNames {
      public const String Id = "Id";
      public const String CustomerName = "CustomerName";
    }
    #endregion EntityPropertyNames
  }
  #endregion Customer class

  #region Order class

  /// <summary>The auto-generated Order class. </summary>
  [DataContract(IsReference=true)]
  [IbEm.DataSourceKeyName(@"BuyABicycleEntities")]
  [IbEm.DefaultEntitySetName(@"BuyABicycleEntities.Orders")]
  public partial class Order : IbEm.Entity {

    /// <summary>Returns the property path for the given expression. </summary>
    /// <example>
    /// Usage:
    /// <code>
    ///    var r = Employee.PathFor(e => e.Manager.City); // returns "Manager.City"
    /// </code>
    /// </example>
    public static string PathFor(System.Linq.Expressions.Expression<System.Func<Order, object>> expr) {
      return IbCore.PropertyPath.For<Order>(expr);
    }

    #region Data Properties

    /// <summary>Gets or sets the Id. </summary>
    [Key]
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="Id", AutoGenerateField=true)]
    [IbVal.RequiredValueVerifier( ErrorMessageResourceName="Order_Id")]
    public int Id {
      get { return PropertyMetadata.Id.GetValue(this); }
      set { PropertyMetadata.Id.SetValue(this, value); }
    }

    /// <summary>Gets or sets the CustomerName. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="CustomerName", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Order_CustomerName")]
    public string CustomerName {
      get { return PropertyMetadata.CustomerName.GetValue(this); }
      set { PropertyMetadata.CustomerName.SetValue(this, value); }
    }

    /// <summary>Gets or sets the CustomerAddress. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="CustomerAddress", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Order_CustomerAddress")]
    public string CustomerAddress {
      get { return PropertyMetadata.CustomerAddress.GetValue(this); }
      set { PropertyMetadata.CustomerAddress.SetValue(this, value); }
    }

    /// <summary>Gets or sets the CustomerPhoneNumber. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="CustomerPhoneNumber", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Order_CustomerPhoneNumber")]
    public string CustomerPhoneNumber {
      get { return PropertyMetadata.CustomerPhoneNumber.GetValue(this); }
      set { PropertyMetadata.CustomerPhoneNumber.SetValue(this, value); }
    }

    /// <summary>Gets or sets the CustomerEmail. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="CustomerEmail", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Order_CustomerEmail")]
    public string CustomerEmail {
      get { return PropertyMetadata.CustomerEmail.GetValue(this); }
      set { PropertyMetadata.CustomerEmail.SetValue(this, value); }
    }

    /// <summary>Gets or sets the BicycleModel. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="BicycleModel", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Order_BicycleModel")]
    public string BicycleModel {
      get { return PropertyMetadata.BicycleModel.GetValue(this); }
      set { PropertyMetadata.BicycleModel.SetValue(this, value); }
    }

    /// <summary>Gets or sets the BicycleSize. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="BicycleSize", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Order_BicycleSize")]
    public string BicycleSize {
      get { return PropertyMetadata.BicycleSize.GetValue(this); }
      set { PropertyMetadata.BicycleSize.SetValue(this, value); }
    }

    /// <summary>Gets or sets the BicycleColour. </summary>
    [DataMember]
    [Bindable(true, BindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="BicycleColour", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=50, IsRequired=true, ErrorMessageResourceName="Order_BicycleColour")]
    public string BicycleColour {
      get { return PropertyMetadata.BicycleColour.GetValue(this); }
      set { PropertyMetadata.BicycleColour.SetValue(this, value); }
    }
    #endregion Data Properties

    #region Navigation properties
    #endregion Navigation properties

    #region EntityProperty definitions
    public partial class PropertyMetadata {

      /// Explicit static constructor ensures static fields are initialized.
      static PropertyMetadata() {}

      /// <summary>The Id <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, int> Id = new IbEm.DataEntityProperty<Order, int>("Id", false, true, IbEm.ConcurrencyStrategy.None, true, null);

      /// <summary>The CustomerName <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, string> CustomerName = new IbEm.DataEntityProperty<Order, string>("CustomerName", false, false, IbEm.ConcurrencyStrategy.None, false, null);

      /// <summary>The CustomerAddress <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, string> CustomerAddress = new IbEm.DataEntityProperty<Order, string>("CustomerAddress", false, false, IbEm.ConcurrencyStrategy.None, false, null);

      /// <summary>The CustomerPhoneNumber <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, string> CustomerPhoneNumber = new IbEm.DataEntityProperty<Order, string>("CustomerPhoneNumber", false, false, IbEm.ConcurrencyStrategy.None, false, null);

      /// <summary>The CustomerEmail <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, string> CustomerEmail = new IbEm.DataEntityProperty<Order, string>("CustomerEmail", false, false, IbEm.ConcurrencyStrategy.None, false, null);

      /// <summary>The BicycleModel <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, string> BicycleModel = new IbEm.DataEntityProperty<Order, string>("BicycleModel", false, false, IbEm.ConcurrencyStrategy.None, false, null);

      /// <summary>The BicycleSize <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, string> BicycleSize = new IbEm.DataEntityProperty<Order, string>("BicycleSize", false, false, IbEm.ConcurrencyStrategy.None, false, null);

      /// <summary>The BicycleColour <see cref="T:IbEm.DataEntityProperty"/>. </summary>
      public static readonly IbEm.DataEntityProperty<Order, string> BicycleColour = new IbEm.DataEntityProperty<Order, string>("BicycleColour", false, false, IbEm.ConcurrencyStrategy.None, false, null);
    }
    #endregion EntityProperty definitions

    #region EntityPropertyNames
    public new partial class EntityPropertyNames : IbEm.Entity.EntityPropertyNames {
      public const String Id = "Id";
      public const String CustomerName = "CustomerName";
      public const String CustomerAddress = "CustomerAddress";
      public const String CustomerPhoneNumber = "CustomerPhoneNumber";
      public const String CustomerEmail = "CustomerEmail";
      public const String BicycleModel = "BicycleModel";
      public const String BicycleSize = "BicycleSize";
      public const String BicycleColour = "BicycleColour";
    }
    #endregion EntityPropertyNames
  }
  #endregion Order class

  #region EntityRelations

  /// <summary>
  /// A generated class that returns the relations between entities in this model.
  /// </summary>
  [IbCore.IdeaBladeGuid("ada4f853-5b03-423a-a51f-0da38018c087", "2.1.4")]
  public partial class EntityRelations : IbEm.IEntityRelations {

    /// Explicit static constructor ensures static fields are initialized.
    static EntityRelations() {}
  }
  #endregion EntityRelations
}

Answer

iamruss picture iamruss · Aug 5, 2014

The error says BuyABicycleEntities should inherit from IDisposable interface. Looks like you are using entity framework, so the database context should already be inheriting it, so please check you're are using the Entity Framework properly.

See here for details - Working with DbContext

UPDATE:

You do not need to wrap your code in using, if you use IdeaBlade EntityManager