I am trying to learn ASP.NET MVC. I apologize in advance for my basic question.
I am having problem trying to create a database by using the model class.
Here is what I have done so far
I created a model class under "Models" folder like so
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace ScripterEngine.Models
{
public class Campaign
{
public int id { get; set; }
public string name { get; set; }
public string layout { get; set; }
public string call_list_connection { get; set; }
public string call_list_table_name { get; set; }
public string status { get; set; }
public string intro_url { get; set; }
public string use_transfer { get; set; }
public string route_callback_to { get; set; }
public string call_list_database_name { get; set; }
public DateTime created_at { get; set; }
public DateTime modified_at { get; set; }
//Initilize the default value
public Campaign()
{
status = "Active";
use_transfer = "No";
route_callback_to = "Self";
}
}
}
Then I created a Campaign Context class in a new folder I created called "DataAccessLayer" like so
using ScripterEngine.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace ScripterEngine.DataAccessLayer
{
public class CampaignContext : DbContext
{
public DbSet<Campaign> Campaign { get; set; }
public CampaignContext() : base("con1")
{
}
}
}
Then I created an initilizer class in a new folder I created called "Seeders" to add dummy data to the table like so
using ScripterEngine.Models;
using ScripterEngine.DataAccessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ScripterEngine.Seeders
{
public class CampaignSeeder : System.Data.Entity.DropCreateDatabaseIfModelChanges<CampaignContext>
{
protected override void Seed(CampaignContext context)
{
var campaigns = new List<Campaign>
{
new Campaign
{
id = 1,
name = "Ahold Landline",
layout = "topmenu",
call_list_table_name = "foo",
call_list_database_name = "bar",
},
new Campaign
{
id = 2,
name = "Ahold Cellphone",
layout = "topmenu",
call_list_table_name = "foo",
call_list_database_name = "bar",
}
};
foreach (var campaign in campaigns)
{
context.Campaign.Add(campaign);
}
context.SaveChanges();
}
}
}
Finally, I added connection string and a contaxt block to my Web.config
//I want the database table to be in the App_Data folder
<connectionStrings>
<add name="con1" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=dbtest;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\dbtest.mdf" providerName="System.Data.SqlClient"/>
</connectionStrings>
and the conext block
<contexts>
<context type="ScripterEngine.DataAccessLayer.CampaignContext" disableDatabaseInitialization="false">
<databaseInitializer type="ScripterEngine.Seeders.CampaignSeeder" />
</context>
</contexts>
But the table does not get created? Do I need to trigger something for the table to generate? What am I doing wrong for the table not to create?
Additionally, I am trying to understand the right way of organizing my application. Is it correct to have a separate context class for every model or I should have one context class for every model? In another words, should context class represent the table or the database itself?
Thank you