Reputation: 15666
I'm doing database first and EF has generated a Code First DbContext subclass...
//------------------------------------------------------------------------------
// <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 Glossary.UI.Database
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class GlossaryDb : DbContext
{
public GlossaryDb()
: base("name=GlossaryDb")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<GlossaryTerm> GlossaryTerms { get; set; }
}
}
As you can see its throwing an exception so I need to comment this out every time the code gets regenerated! More to the point though, why has it generated this class?
Upvotes: 0
Views: 351
Reputation: 2246
You need a context in order to use Entity Framework. You can either write one yourself or you can use the tool to automatically generate it. With newer versions of EF using database first (.EDMX and designer) it will automatically generate a DbContext for you.
The DbContext you showed above is NOT a "Code First" context as it has the UnintentionalCodeFirstException. If that exception is being thrown then it suggest you are trying to use code first mode.
Check the database exists and check the connection strings. Don't use code first by mistake.
Upvotes: 3