c# – 数据库没有创建记录
作者:互联网
使用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);
}
我究竟做错了什么? ……新手…
解决方法:
首先检查连接字符串.
确保这是在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();
}
标签:c,entity-framework-5 来源: https://codeday.me/bug/20190709/1409162.html