我有以下代碼:C# - 不能投實體接口實體
public class CoursesController : ApiController
{
private ITanulmanyokEntities _entities;
public CoursesController()
: this(new TanulmanyokEntities()) // ERROR HERE
{
}
public CoursesController(ITanulmanyokEntities entities)
{
if (entities == null)
throw new ArgumentNullException("entities");
_entities = entities;
}
}
在這裏,我做了一個接口:
public interface ITanulmanyokEntities : IDisposable
{
Int32 SaveChanges();
DbSet<Application> Applications { get; set; }
DbSet<Course> Courses { get; set; }
DbSet<EducationSubject> EducationSubjects { get; set; }
DbSet<EducationUser> EducationUsers { get; set; }
DbSet<Program> Programs { get; set; }
}
這應該是從生成的實體的接口數據庫:
public partial class TanulmanyokEntities : DbContext
{
public TanulmanyokEntities()
: base("name=TanulmanyokEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Application> Applications { get; set; }
public virtual DbSet<Course> Courses { get; set; }
public virtual DbSet<EducationSubject> EducationSubjects { get; set; }
public virtual DbSet<EducationUser> EducationUsers { get; set; }
public virtual DbSet<Program> Programs { get; set; }
}
但是,我似乎無法將一個轉換成另一個。請參閱評論行。問題是,我的編譯器不能轉換接口/真實的東西。我想我實現了所有必須實現的東西(我也遵循一些有用的示例代碼)。我錯過了什麼?
您無法將TanulmanyokEntities轉換爲ITanulmanyokEntities,因爲它沒有實現接口。這是什麼接口? – anserk
這是'TanulmanyokEntities'的界面。老師的示例代碼完全相同,並且適用於他...我現在正在運行該代碼。 –
你可以發佈示例鏈接嗎? – anserk