Reputation: 369
This is the content of my DivorceCases.cs file inside Models:
public class DivorceCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class DivorceCasesContext : DbContext
{
public DivorceCasesContext() : base("mssqlDB") { }
public DbSet<DivorceCases> DivorceCase { get; set; }
}
This is the content of my CorporationCases.cs file inside Models:
public class CorporationCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class CorporationCasesContext : DbContext
{
public CorporationCasesContext() : base("mssqlDB") { }
public DbSet<CorporationCases> CorporationCase { get; set; }
}
Now my question is:
I am using code-first approach to let EF6 create table for me automatically.
when I try to create and use any instance of table and EF6 creates it as well for DivorceCases Model
and Context pair. But after the DivorceCases
table has been created, I try to create and use CorporationCases
Model instance then EF6 fails to automatically create the table for me because
"Transaction" Table has already been created by DivorceCases context
So how do I solve this issue?
Upvotes: 1
Views: 56
Reputation: 65978
You don't need to create 2 dbContext
here.just use one as shown below.
Important Note : You're not following basic naming conventions.I highly recommend to use those.
one place is here : public DbSet<DivorceCases> DivorceCase { get; set; }
need to be corrected as public DbSet<DivorceCase> DivorceCases { get; set; }
Using One DbContext :
public class DivorceCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class CorporationCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class YourCasesContext : DbContext
{
public YourCasesContext () : base("mssqlDB") { }
public DbSet<DivorceCases> DivorceCase { get; set; }
public DbSet<CorporationCases> CorporationCase { get; set; }
}
Upvotes: 1