数据库未创建记录

本文关键字:记录 创建 数据库 | 更新日期: 2023-09-27 18:21:04

使用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新手。。。。

数据库未创建记录

首先检查连接字符串。

请确保这是在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();
        }