4

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

数据库上下文:

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

4

1 回答 1

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();
        }
于 2013-07-09T18:42:26.797 回答