C# 使用时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13553889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error using Using
提问by user1851379
I have an error
我有一个错误
Type used in a using statement must be implicitly convertible to 'System.IDisposable'
using 语句中使用的类型必须可隐式转换为“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...
EntityContainer() 看起来像这样......
//------------------------------------------------------------------------------
// <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.
编辑: DbContext 实现了 IDisposable 并且我无法编辑 EntityContainer() 类,因为它是从模板生成的。
Any ideas what is wrong?
任何想法有什么问题?
回答by Rui Jarimba
If you want to use EntityContainerin an usingstatement, then it must implement IDisposable
如果要EntityContainer在using语句中使用,那么它必须实现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();
}
}
回答by Don Kirkby
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.
上下文仍然实现 IDisposable,但如果您收到错误...你的项目。
回答by Brandon Ward
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 IUnitOfWorkinterface:
然后你的部分 ObjectContext 可以实现IUnitOfWork接口:
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):
此方法允许我使用 using 指令而不会出错。使用示例(我将 Repository 模式与此结合使用):
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.
希望这会有所帮助。
回答by Rami Sarieddine
The answer would be to add the EntityFramework assembly to the references
答案是将 EntityFramework 程序集添加到引用中
回答by James Skemp
We have a page that throws this error and the does not contain a definition for SaveChangeserror. Entities are defined in a separate project, and all the necessary references appear to be used.
我们有一个页面会抛出这个错误和does not contain a definition for SaveChanges错误。实体在单独的项目中定义,并且似乎使用了所有必要的引用。
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+.
我发现在编辑页面时从 Visual Studio 2012 切换到 2010 就足够了。这绝对不理想,但它可以工作,直到我们将所有内容都切换到 EF 5+。
回答by Demodave
I added entity framework to my calling project's references and it fixed my issue.
我在调用项目的引用中添加了实体框架,它解决了我的问题。
回答by 2old4this
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.
如果您使用 Visual Studio 2013 遵循“Pro C# 5.0 和 .Net 4.5 框架”第 23 章中的示例,则只需在 AutoLotDAL 文件夹中添加其他 DLL 作为参考。

