Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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.

What I have tried:

My Web.Config
configuration>
<configsections>
<!-- 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
C#
//The MailType enum is the driving factor for email template engine. This is basically a vocab for all possible email types
   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; }

       //-------------------   Foreign Key to GQ table  -------------------------------------------------------//
       public int DetailedQuestionID { get; set; }
       public virtual DetailedQuestion DetailedQuestions { get; set; }

       //-------------------  collection of uploaded documents associated with this GQ  ----------------------//
       public virtual ICollection<FSSTSSDucument> FSSTSSDucuments { get; set; }
       public virtual ICollection<SRDDRDDucument> SRDDRDDucuments { get; set; }
       public virtual ICollection<SolutionArchitectureDucument> SolutionArchitectureDucuments { get; set; }

   }





My DBCotext

C#
public class GeneralQuestionContext : DbContext
    {
        public GeneralQuestionContext()
            : base("GeneralQuestionContext")
           // was : base("DBConnectionString")
        {
        }

        //properties to retrieve table entities
        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

C#
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

C#
public class Global : HttpApplication
  {
      void Application_Start(object sender, EventArgs e)
      {
          // Code that runs on application startup
          BundleConfig.RegisterBundles(BundleTable.Bundles);
          AuthConfig.RegisterOpenAuth();
          //RouteConfig.RegisterRoutes(RouteTable.Routes);

          //--  Tell the DB which initial model class to use  --//
         Database.SetInitializer<GeneralQuestionContext>(new GeneralQuestionModelInitializer());
      }




my Save Button click

C#
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.GeneralQuestions.Add(objGQ);
                   _db.SaveChanges();

                   lblMsg.ForeColor = Color.Green;
                   lblMsg.Text = "Record Saved Successfully.";

                   Response.Redirect("Database_Details.aspx");
               }
               else
               {
                  // lblMsg.Text = "Validation failed - Not processed.";
                   lblMsg.ForeColor = Color.Red;
               }
           }
           catch (Exception exx)
           {
               Response.Write(exx.Message);
           }
           finally
           {
               Utilities.ResetAllControls(this);
           }
       }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900