Entity structure creating a separate database corresponding to each class in the model

I started using the Entity framework in ASP.NET MVC4.

I created 3 classes in my Model folder and created controllers for each model.

Now, when I launch the application, he created separate databases corresponding to each class of the model.

Can I use only one database?

+4
source share
1 answer

Do you create a separate context for each of the classes?

 public class Employee { [Key] public int EmpId { get; set; } // < Can I make a suggestion here // and suggest you use Id rather than // EmpId? It looks better referring to // employee.Id rather than employee.EmpId [Required] public string Fullname { get; set; } ... } public class AnotherClass { ... } 

And then listing all your models in context:

 public MyDbContext : DbContext { public DbSet<Employee> Employees { get; set; } public DbSet<AnotherClass> AnotherClasses { get; set; } } 

You can also specify the name of the connection string using the constructor in the Context:

 public MyDbContext() : base("ConnectionString") { } 

It is important that all your models are in the same context.

Use of context

 var context = new MyDbContext(); var employees = context.employees.ToList(); // I prefer storing the data as an IList 

This tells EF to query the database and store the data inside the employee variable.

+1
source

All Articles