Non Recording Database

Inserting a record into the database using EF 5.0 does not work. Debugging through my code I cannot find any exception. Looking through the code, it should insert a new record into the database, but when I look at the database, no new records are created. However, I made changes to my database and in debugging, when I check the "assestUser" (model), I can see all the values, including the deleted columns (the changes I made). Can someone please help me on what I am doing wrong? This is what I did:

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 ....

+4
source share
1 answer

First check the connection string.

make sure it is on Global.ASAX:

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

use this code:

  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(); } 
+1
source

Source: https://habr.com/ru/post/1489687/


All Articles