数据库没有创建记录

时间:2013-07-04 11:11:37

标签: c# entity-framework-5

使用EF 5.0在数据库中插入记录无效。通过我的代码调试我找不到任何异常。通过代码它应该在数据库中插入一条新记录,但是当我查看数据库时,没有创建新记录。但是,我确实对我的数据库进行了更改,当我检查“assestUser”(模型)时,我可以看到所有值,包括已删除的列(我所做的更改)。有人可以帮我解决我做错的事吗? 这就是我所做的:

页:

   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);
    }

我做错了什么? ......对ef ....新的....

1 个答案:

答案 0 :(得分:1)

首先检查连接字符串。

确保这是在Global.ASAX:

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

使用这样的代码:

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