I'm getting this error as soon as I try and initialize my DBContext Short background : I'm using a shared dll which contains DBcontext entity framework with all entites, this is referenced in my project along with enityframework.dll etc.
I can see all the classes within and here is the code in the Model.dll
namespace Model
{
public class BSysDbContext : DbContext
{
public BSysDbContext(bool proxyCreationEnabled = true);
public virtual DbSet<TaskResource> TaskResources { get; set; }
public virtual DbSet<TaskOrder> TaskOrders { get; set; }
public virtual DbSet<TaskMessage> TaskMessages { get; set; }
public virtual DbSet<TaskEvent> TaskEvents { get; set; }
etc
In my code I'm trying to retrieve the data and it gets the above error when hitting this "_context = new BSysDbContext();"
public partial class ChangeSupervisor : DevExpress.XtraEditors.XtraForm
{
BSysDbContext _context;
public ChangeSupervisor()
{
InitializeComponent();
_context = new BSysDbContext();
}
}
My connection string is :
<connectionStrings>
<add name="BSysDbContext" connectionString="metadata=res://Model.dll/BSysDbModel.csdl|
res://Model.dll/BSysDbModel.ssdl|
res://Model.dll/BSysDbModel.msl
;provider=System.Data.SqlClient;provider connection string=';data source=DESKTOP-5T9EDLN;initial catalog=BuilderSysDB_DEV;user id=llduser;password=P@ssw0rd1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';" providerName="System.Data.EntityClient" />
</connectionStrings>
No it doesn't matter what I put in the connection string, it doesn't seem to even know the string exists. According to what I've googled I don;t need to have the model.dll like this
public class BSysDbContext : DbContext
{
public BSysDbContext ()
: base("name=BSysDbContext")
{
}
}
Since the name is the same.
NB, I don't have access to the source of the DLL otherwise I would have made this change. All I need to know is... Is the missing "public BSysDbContext () : base("name=BSysDbContext") " in the DLL what is causing my error message or am I missing something here?
The connection string looks ok to me, I created another blank project and used that as a sample to create this one
Any advice is appreciated
Ok, so after a long trial and error period, the solution (that was obviously not known to me as a newbie to entity framework and migration dll's) was that even though the separate dll which holds the entity models has already got the context and definitions, you also need to have your own dbcontext class in your project with all the tables
public class BSysDbContext : DbContext
{
public BSysDbContext()
: base("Name=BSysDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<tblJob>().ToTable("tblJob", "Entity");
modelBuilder.Entity<TaskOrder>().ToTable("TaskOrder", "Entity");
modelBuilder.Entity<Project>().ToTable("Project", "Entity");
modelBuilder.Entity<Order>().ToTable("Order", "Entity");
modelBuilder.Entity<OrderCode>().ToTable("OrderCode", "Entity");
modelBuilder.Entity<ProjectTask>().ToTable("ProjectTask", "Entity");
modelBuilder.Entity<Model.Task>().ToTable("Task", "Entity");
modelBuilder.Entity<tblSupervisor>().ToTable("tblSupervisor", "Entity");
modelBuilder.Entity<tblSupplier>().ToTable("tblSupplier", "Entity");
modelBuilder.Entity<JobProject>().ToTable("JobProject", "Entity");
base.OnModelCreating(modelBuilder);
}
public virtual DbSet<tblJob> Jobs { get; set; }
public virtual DbSet<TaskOrder> TaskOrders { get; set; }
public virtual DbSet<Project> Projects { get; set; }
public virtual DbSet<Order> Orders { get; set; }
etc....