我在 asp.net mvc3 中使用实体框架,当我尝试向表中添加新记录时出现此错误。这是我的代码
namespace CvGenerator.Models
{
public class LogInEntities:DbContext
{
public DbSet LogIn { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Entity<LogIn>().ToTable("LogInData");
base.OnModelCreating(modelBuilder);
}
}
}
模型控制器:
public class LogIn
{
public int LogInId { get; set; }
public string NameAndSurname { get; set; }
public string Email { get; set; }
public virtual ICollection<LogIn> LogInCol { get; set; }
}
我使用ajax调用来记录数据的方法:
public void LogIn(string email,string nameAndSurname)
{
LogInEntities logIndb = new LogInEntities();
LogIn logIn = new LogIn();
if(ModelState.IsValid)
{
logIn.Email = email;
logIn.NameAndSurname = nameAndSurname;
***logIndb.LogIn.Add(logIn);***
logIndb.SaveChanges();
}
}
在标记的行上,我得到了例外