我试图学习在这里的教程在网页api上进行Google+身份validation
我有课 :
public partial class MyApplicationDbContext : DbContext { public MyApplicationDbContext() : base("name=MyApplicationDbContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Appointment> Appointment { get; set; } public virtual DbSet<Users> Users { get; set; } }
我有对象
MyApplicationDbContext db = new MyApplicationDbContext();
和
我有这个方法
public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(db.Create); //line 10 }
IAppBuilder来自OWIN。
我从第10行收到一条消息:
错误CS1061“MyApplicationDbContext”不包含“Create”的定义,并且没有find接受types为“MyApplicationDbContext”的第一个参数的扩展方法“Create”(您是否缺lessusing指令或程序集引用?)BS.HealthCare。应用程序E:\ BS.HealthCareApplication \ BS.HealthCare.Application \ BS.HealthCare.Application \ Controllers \ UsersController.cs 137活动
有什么我错过了吗?
我假设你缺少Create
方法:
public class MyApplicationDbContext : DbContext { public MyApplicationDbContext() : base("your_connection_name") { } public static MyApplicationDbContext Create() { return new MyApplicationDbContext(); } }
编辑:
public partial class MyApplicationDbContext : DbContext { public MyApplicationDbContext() : base("name=MyApplicationDbContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public static MyApplicationDbContext Create() { return new MyApplicationDbContext(); } public virtual DbSet<Appointment> Appointment { get; set; } public virtual DbSet<Users> Users { get; set; } }
你需要在你的MyApplicationDbContext上定义Create方法
public partial class MyApplicationDbContext : DbContext { public MyApplicationDbContext() : base("name=MyApplicationDbContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public static MyApplicationDbContext Create() { return new MyApplicationDbContext(); } public virtual DbSet<Appointment> Appointment { get; set; } public virtual DbSet<Users> Users { get; set; } }
那么在你的Startup类中调用你的MyApplicationDbContext的Create方法就好
public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(MyApplicationDbContext.Create); }