17

I have an error

Type used in a using statement must be implicitly convertible to 'System.IDisposable'

on line

using (var context = new EntityContainer())

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Globalization;
using System.Data;
using System.Data.Entity;
using school.usi.susibar.model;

namespace school.usi.susibar.test
{
    class Program
    {

        public static void Main(string []args)
         {
            using (var context = new EntityContainer())
            {

                addOrderStatusType(context);

                Console.ReadLine();
            }
        } 


        private static void addOrderStatusType(EntityContainer context)
        {
            try
            {
                OrderStatusType type = new OrderStatusType
                {
                    Name = "Vyrizeno", 
                    CancelPermission = false, 
                    ChangePermission = false
                };
                context.OrderStatusTypes.Add(type);
                context.SaveChanges();
                Console.WriteLine("Pridano");
            }
            catch (Exception ex) {
                Console.WriteLine(ex.InnerException);
            }
        }
         }

            }

The EntityContainer() looks like this...

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace school.usi.susibar.model
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class EntityContainer : DbContext
    {
        public EntityContainer()
            : base("name=EntityContainer")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Role> Roles { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Table> Tables { get; set; }
        public DbSet<OrderStatusType> OrderStatusTypes { get; set; }
        public DbSet<Person> Persones { get; set; }
        public DbSet<Item> Items { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Stock> Stocks { get; set; }
        public DbSet<ItemOrderList> ItemOrderLists { get; set; }
        public DbSet<ItemOrderStatus> ItemOrderStatuses { get; set; }
        public DbSet<PaymentOrderStatus> PaymentOrderStatuses { get; set; }
        public DbSet<Prepaid> Prepaids { get; set; }
    }
}

EDIT: DbContext implements IDisposable and I cant edit EntityContainer() class since its generated from a template.

Any ideas what is wrong?

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
user1851379
  • 171
  • 1
  • 1
  • 4
  • 2
    Look at this: http://stackoverflow.com/questions/12508031/entity-framework-5-model-first-where-is-idisposable-gone – James Gaunt Nov 25 '12 at 18:42

7 Answers7

59

From this answer:

The context still implements IDisposable, but if you're getting an error... complaining about not implementing IDisposable, then your problem is most likely that your model is defined in a separate assembly that references EF5 and you have not added an EF5 reference to your project.

Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
7

The answer would be to add the EntityFramework assembly to the references

Rami Sarieddine
  • 5,396
  • 35
  • 41
5

If you want to use EntityContainer in an using statement, then it must implement IDisposable

Try this:

public partial class EntityContainer : DbContext, IDisposable
{
    public EntityContainer()
        : base("name=EntityContainer")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Role> Roles { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Table> Tables { get; set; }
    public DbSet<OrderStatusType> OrderStatusTypes { get; set; }
    public DbSet<Person> Persones { get; set; }
    public DbSet<Item> Items { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Stock> Stocks { get; set; }
    public DbSet<ItemOrderList> ItemOrderLists { get; set; }
    public DbSet<ItemOrderStatus> ItemOrderStatuses { get; set; }
    public DbSet<PaymentOrderStatus> PaymentOrderStatuses { get; set; }
    public DbSet<Prepaid> Prepaids { get; set; }

    public void Dispose()
    {
        base.Dispose();
    }
}
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
  • 3
    Yeah but IDisposable is implemented by DbContext. So why is it not working? – user1851379 Nov 25 '12 at 18:21
  • For some reason you need to implement `IDisposable` in the subclass as well. Try the code that I added to my answer – Rui Jarimba Nov 25 '12 at 18:33
  • I cant add , IDisposable since class EntityContainer is autogenerated and i cant edit it :( – user1851379 Nov 25 '12 at 18:34
  • The generated class is partial, so do like this: Create a class named `EntityContainer.IDisposable.cs` and copy/paste this code: `public partial class EntityContainer : IDisposable { public void Dispose() { base.Dispose(); }` – Rui Jarimba Nov 25 '12 at 18:41
  • 7
    You shouldn't need to add IDisposable to a subclass if it's defined on a base class. The problem here is more likely to do with referencing incompatible versions of EF. See http://stackoverflow.com/questions/12508031/entity-framework-5-model-first-where-is-idisposable-gone – James Gaunt Nov 25 '12 at 18:46
  • I dont understand this part... Click on the designer surface of your EDMX file. What is a designer surface? – user1851379 Nov 25 '12 at 19:01
  • 1
    This can't be correct answer, because `DbContext` already implements `IDisposable` – andreister Jun 21 '13 at 12:30
  • This is not the answer for this question. It's http://stackoverflow.com/a/13554255/68571. – VansFannel Jun 08 '14 at 07:35
4

I added entity framework to my calling project's references and it fixed my issue.

Demodave
  • 6,242
  • 6
  • 43
  • 58
1

You could try to use the Unit of Work pattern, something like this.

public interface IUnitOfWork : IDisposable {
    void Save();
}

Then your partial ObjectContext could implement the IUnitOfWork interface:

public partial class EntityContainer : IUnitOfWork {
    public void Save() {
        this.SaveChanges();
    }

    void IDisposable.Dispose() {
        base.Dispose();
    }
}

This method allows me to use the using directive without errors. Example of use (I use the Repository pattern in conjunction with this):

    public List<OrderModelView> GetAllOrdersAscByName() {

        using (IUnitOfWork context = new EntityContainer()) {
            Repository<Order> orderRepository = new Repository<Order>(context);

            var orders = orderRepository.GetAll()
                .Select(o => new OrderModelView {
                    OrderID = o.OrderID,
                    Name = o.Name,
                    OrderTypeCount = o.OrderTypes.Count
                })
                .OrderBy(o => o.Name)
                .ToList();

            return orders;
        }
    }

Hopefully this helps.

Brandon Ward
  • 143
  • 3
  • 7
0

We have a page that throws this error and the does not contain a definition for SaveChanges error. Entities are defined in a separate project, and all the necessary references appear to be used.

I've found that switching from Visual Studio 2012 to 2010 is sufficient when editing the page. It's definitely not ideal, but it works until we switch everything over to EF 5+.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
0

If you are following the example in Chapter 23 of "Pro C# 5.0 and the .Net 4.5 Framework" using Visual Studio 2013 then just add the other DLLs in the AutoLotDAL folder as references.

2old4this
  • 31
  • 1
  • 3