Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using Using [duplicate]

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?

like image 514
user1851379 Avatar asked Nov 25 '12 18:11

user1851379


People also ask

What is a duplicate error?

Contains information about an error that occurred when an attempt was made to save a duplicate record. Use if your organization has set up duplicate rules, which are part of the Duplicate Management feature.

How do I fix error 1062 in MySQL?

1062 - Duplicate Entry To solve this, Set the primary key column as AUTO_INCREMENT . And when you are trying to insert a new row, ignore the primary key column or insert NULL value to primary key.

What causes duplicate records?

Data aggregation and human typing errors are some of the sources of duplicate data. Customers may also provide a company with different information at different points in time. Hence, businesses should consider removing duplicate records from their Database.


1 Answers

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.

like image 196
Don Kirkby Avatar answered Oct 19 '22 11:10

Don Kirkby