The context cannot be used while the model is being created

前端 未结 14 1391
渐次进展
渐次进展 2020-12-03 13:27

In my application I receive the following error:

The context cannot be used while the model is being created.

I\'m not sure what

14条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 14:02

    Add DatabaseContext constructor with database catalog name.

    public class DatabaseContext : DbContext {
        public DbSet Products { get; set; }
    
        public DatabaseContext() : base("ProjectCode") {}
    }
    

    Change entityframework config in app.config like this.

    
    

    Add Annotations to Products class. Make sure your database has a "Products" table with tow fields "ProductId" and "ProductName".

    class Products {
        [Key]
        public int ProductID { get; set; }
        public string ProductName { get; set; }
    }
    

    If still error, try to change you project target framework to use .net framework 4.0.

提交回复
热议问题