I am getting following error message when context is trying to connect to database using code-first approach of Entity Framework
System.Data.Entity.DbContext' does not contain a definition for 'System' and no extension method 'System' accepting a first argument of type 'System.Data.Entity.DbContext' could be found (are you missing a using directive or an assembly reference?)
Code:
public class Context : DbContext, IDisposable
{
public Context() : base("EcommConnectionString")
{
}
public List<Entities.RLI_State> States { get; set; }
public List<Entities.RLI_Product> Products { get; set; }
public List<Entities.RLI_StateProduct_List> StateProductList { get; set; }
}
This is how my connection string defined in web.config
:
<appSettings>
<add key="EcommConnectionString"
value="data source=localhost;initial catalog=tempdb;integrated security=SSPI" />
</appSettings>
There are a couple of problems here.
Firstly, DbContext already imlpements IDisposable so you don't need that. It's not a problem having it there, but it is redundant.
Secondly your List<>
properties should be using DbSet<>
instead:
public class Context : DbContext
{
public Context() : base("EcommConnectionString")
{
}
public DbSet<Entities.RLI_State> States { get; set; }
public DbSet<Entities.RLI_Product> Products { get; set; }
public DbSet<Entities.RLI_StateProduct_List> StateProductList { get; set; }
}
Finally, as @marc_s mentioned, the connection string in your web.config should be in the <connectionStrings>
element, not in <appSettings>
:
<connectionStrings>
<add name="EcommConnectionString"
connectionString="data source=localhost;initial catalog=tempdb;integrated security=SSPI" />
</connectionStrings>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.