我完全是实体框架的开端人,我困惑不解,不可能使实体框架产生数据库。
我的背景是:
public class ChatContext : DbContext
{
public ChatContext()
: base("ChatContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ChatRoom>()
.Map(c => c.ToTable("chat_rooms"));
modelBuilder.Entity<ChatUser>()
.HasMany(u => u.Rooms)
.WithMany(r => r.Users)
.Map(c => c.ToTable("chat_users"));
modelBuilder.Entity<ChatUser>()
.HasMany(u => u.ConnectedUsers);
base.OnModelCreating(modelBuilder);
}
public DbSet<ChatUser> Users { get; set; }
public DbSet<ChatRoom> Rooms { get; set; }
public DbSet<ChatMessage> Messages { get; set; }
}
我的全球协会:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
protected void Application_Start()
{
Database.SetInitializer<ChatContext>(new DropCreateDatabaseIfModelChanges<ChatContext>());
RegisterRoutes(RouteTable.Routes);
}
}
我在网上的联系:
<connectionStrings>
<add name="ChatContext" connectionString="Data Source=.SQLEXPRESS;Initial Catalog=ChatContext;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Can anyone spot where the problem lies? When I build the project, no errors are returned. A break point in Global.axax shows it hits the Application_Start().