I'm learning and working at once. A simple tutorial taught me to make a database using code first. Now I want several tables but I end up with several databases. I'm pretty sure that's wrong, right?
Example: I have one database for users called "DefaultConnection" with tables like "AspNetRoles", "AspNetUsers" and so on. Great.
But... I have a separate database for blogs called "BlogDBContext" with a single table. I have another database for lessons called "LessonDBContext" with a single table for lessons. My friend says that's wrong and I think he's right, but he keeps breaking things when he tries to fix it.
What is the correct way to use code-first to set up tables for different models without creating whole new databases?
FWIW I think this is about where I'm screwing up: general secion of the tutorial I got it from
I'm doing something like this for each one:
namespace ScatterSchool.Models
{
public class Lesson
{
public int Id { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string Tags { get; set; }
[Display(Name = "Lesson Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime LessonDate { get; set; }
//public virtual User User { get; set; }
}
public class LessonDBContext : DbContext
{
public DbSet<Lesson> Lessons { get; set; }
}
}
I set up connection strings in Web.Config, etc... And I get a new database with a Lessons table...
Not sure what he's doing but he gets one database with lessons, blogs, and users tables and everything is broken. His code looks similar but he's isolating all the DbContexts:
namespace ScatterSchool.Models {
public partial class Lesson {
public int ID { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string Tags { get; set; }
public string DateCreated { get; set; }
public int User_Id { get; set; }
public virtual User User { get; set; }
}
}
and the contexts:
namespace ScatterSchool.Models {
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public partial class DatabaseContext : DbContext {
public DatabaseContext() : base("name=Database") { }
public virtual DbSet<Blog> Blogs { get; set; }
public virtual DbSet<Lesson> Lessons { get; set; }
public virtual DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<User>()
.HasMany(e => e.Lessons)
.WithRequired(e => e.User)
.HasForeignKey(e => e.User_Id);
}
}
}
I make stuff that's not broken, but I think I'm doing it more wrong. He makes stuff that's broken, but I think he's doing something more right.
I just want some general guidance or tips on properly setting up the background data for each of my models.
A final but important question: Should I leave the DefaultConnection database with all the vital secure login stuff alone and make a new one for everything else, or just add tables to that database? I eventually want to show different blogs/lessons for different users.