I am using ASP.NET and EF 5.0. I've used NuGet to install the EntityFramework package, Visual studio 2012
the database is never created, and again Data is not saved if i manually create a table.I am trying to use Code First, Please help.
<!-- DBConnectionString For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<connectionstrings>
<add name=" DBConnectionString" connectionstring="Data Source=24088DEVJNB001V;Initial Catalog=CSM;User ID=retailbank;Password=F122popstest; Integrated Security=SSPI;">
providerName="System.Data.SqlClient" />
<add name="IPP_TestConnectionString" connectionstring="Data Source=24088DEVJNB001V;Initial Catalog=IPP_Test;Persist Security Info=True;User ID=retailbank;Password=F122popstest">
providerName="System.Data.SqlClient" />
my class
public enum MailType
{
Unknown,
Success,
Failure
}
public class GeneralQuestion
{
[Key]
public int GeneralQuestionID { get; set; }
public string ProjectStream { get; set; }
public bool? PBBIT { get; set; }
public bool? CIBIT { get; set; }
public bool? ROA { get; set; }
public bool? GEF { get; set; }
public bool? International { get; set; }
public DateTime CreatedOn { get; set; }
public int DetailedQuestionID { get; set; }
public virtual DetailedQuestion DetailedQuestions { get; set; }
public virtual ICollection<FSSTSSDucument> FSSTSSDucuments { get; set; }
public virtual ICollection<SRDDRDDucument> SRDDRDDucuments { get; set; }
public virtual ICollection<SolutionArchitectureDucument> SolutionArchitectureDucuments { get; set; }
}
My DBCotext
public class GeneralQuestionContext : DbContext
{
public GeneralQuestionContext()
: base("GeneralQuestionContext")
{
}
public DbSet<GeneralQuestion> GeneralQuestions { get; set; }
public DbSet<DetailedQuestion> DetailedQuestions { get; set; }
public DbSet<FSSTSSDucument> FSSTSSDucuments { get; set; }
public DbSet<SRDDRDDucument> SRDDRDDucuments { get; set; }
public DbSet<SolutionArchitectureDucument> SolutionArchitectureDucuments { get; set; }
}
My ModelInitializer
public class GeneralQuestionModelInitializer : DropCreateDatabaseIfModelChanges<GeneralQuestionContext>
{
protected override void Seed(GeneralQuestionContext context)
{
GetGeneralQuestions().ForEach(g => context.GeneralQuestions.Add(g));
base.Seed(context);
}
private static List<GeneralQuestion> GetGeneralQuestions()
{
var generalQuestions = new List<GeneralQuestion> {
new GeneralQuestion
{
GeneralQuestionID = 1,
ProjectStream = "Testing Project Stream 1",
PBBIT = true,
CIBIT = true,
ROA = false,
GEF = false,
International = false,
CreatedOn = DateTime.Now
},
new GeneralQuestion
{
GeneralQuestionID = 2,
ProjectStream = "Testing Project Stream 2",
PBBIT = false,
CIBIT = false,
ROA = false,
GEF = false,
International = false,
CreatedOn = DateTime.Now
}
};
return generalQuestions;
}
}
My Global.asax
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterOpenAuth();
Database.SetInitializer<GeneralQuestionContext>(new GeneralQuestionModelInitializer());
}
my Save Button click
private void SaveAndProcess()
{
try
{
if (ValidateGeneralQuestion())
{
string currSession = Session["Name"].ToString();
GeneralQuestion objGQ = new GeneralQuestion();
objGQ.ProjectStream = ProjectList.SelectedValue;
objGQ.CreatedOn = DateTime.Now;
if (inputCIBIT.Checked)
{
objGQ.CIBIT = true;
}
if (inputGEF.Checked)
{
objGQ.GEF = true;
}
if (inputPBBIT.Checked)
{
objGQ.PBBIT = true;
}
if (inputROA.Checked)
{
objGQ.ROA = true;
}
if (inputInternational.Checked)
{
objGQ.International = true;
}
_db.SaveChanges();
lblMsg.ForeColor = Color.Green;
lblMsg.Text = "Record Saved Successfully.";
Response.Redirect("Database_Details.aspx");
}
else
{
lblMsg.ForeColor = Color.Red;
}
}
catch (Exception exx)
{
Response.Write(exx.Message);
}
finally
{
Utilities.ResetAllControls(this);
}
}