4

Inserting a record in the database using EF 5.0 isn't working. Debugging through my code I cannot find any exception. Going through the code it should insert a new record in the database, but when I look at the database there are no new records created. However, I did make changes to my database, and in debug when I check on the "assestUser" (the model) I can see all values, including the deleted columns (the changes I made). Could somebody please help me with what I am doing wrong? This is what I have done:

Page:

   public partial class _Default : Page
{
    EntityContext context;

    public _Default()
    {
        context = new EntityContext();
    }

     protected void btnSavePersonalDetails_Click(object sender, EventArgs e)
    {
        try
        {
            SkillsAssestUser assestUser = new SkillsAssestUser();
            assestUser.DomainAcc = lblDomAcc.Text;
            assestUser.Name = txtName.Text;
            assestUser.Surname = txtSurname.Text;
            assestUser.Division = txtDivision.Text;
            assestUser.Manager = txtManager.Text;


            context.SkillsAssestUsers.Add(assestUser);
            context.SaveChanges();


            //var assestUser = context.Set<SkillsAssestUser>();
            //assestUser.Add(new SkillsAssestUser
            //{
            //    DomainAcc = lblDomAcc.Text,
            //    Name = txtName.Text,
            //    Surname = txtSurname.Text,
            //    Division = txtDivision.Text,
            //    Manager = txtManager.Text
            //});

            //context.SaveChanges();

            ClearPersonalDetails();
        }
        catch (Exception ex)
        {
            throw new Exception("Error inserting Details. " + ex.Message);
        }
    }

DbContext:

 public partial class EntityContext : DbContext
 {
    public EntityContext()
        : base("name=SOSConnectionString")
    {
        base.Configuration.LazyLoadingEnabled = true;
        base.Configuration.ProxyCreationEnabled = false;
    }

     #region AddTables
    public DbSet<AdditionalSkills> AdditionalSkillss { get; set; }
    public DbSet<CertifiedTraining> CertifiedTrainings { get; set; }
    public DbSet<OtherCertifiedTraining> OtherCertifiedTrainings { get; set; }
    public DbSet<OtherEdu> OtherEdus { get; set; }
    public DbSet<SchoolEdu> SchoolEdus { get; set; }
    public DbSet<SkillsAssestUser> SkillsAssestUsers { get; set; }
    public DbSet<ValueAddedSkills> ValueAddedSkillss { get; set; } 
    #endregion
    }

     void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        AuthConfig.RegisterOpenAuth();
        Database.SetInitializer<EntityContext>(null);
    }

What am I doing wrong? ...new to ef....

7
  • IMO, worth asking, are you copying the database file to the output directory on every build or do you use a SQL Server? Commented Jul 4, 2013 at 11:30
  • @Alex, Hi, no I'm not copying db files to the output directory on every build... I've used ef 4.1 in an windows application before, and this worked there.... Commented Jul 4, 2013 at 11:34
  • Have you checked the types of the values you are assigning? Commented Jul 4, 2013 at 13:17
  • Yes, all the types are correct.... Commented Jul 4, 2013 at 13:18
  • well the code seems correct..If possible(i know its not right way) but can u again regenerate the ef from scratch.. Commented Jul 4, 2013 at 13:26

1 Answer 1

1

Check Connection String First.

be sure that this is on Global.ASAX:

void Application_Start(object sender, EventArgs e)
 {
    // Code that runs on application startup
    AuthConfig.RegisterOpenAuth();
    Database.SetInitializer<EntityContext>(null);
 }

use code like this:

      using (var mycontext = new context())
        {
           SkillsAssestUser assestUser = new SkillsAssestUser();
           assestUser.DomainAcc = lblDomAcc.Text;
           assestUser.Name = txtName.Text;
           assestUser.Surname = txtSurname.Text;
           assestUser.Division = txtDivision.Text;
           assestUser.Manager = txtManager.Text;

           mycontext.SkillsAssestUsers.Add(assestUser);
           mycontext.SaveChanges();
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.