我有以下父类,它有多个子类。我已成功创建并从SQL数据库中检索数据。现在我想使用FormCollection
更新记录。
public class UserDetailContext : DbContext
{
public UserDetailContext()
: base("DefaultConnection")
{
}
public DbSet<UserDetails> UserDetails { get; set; }
public DbSet<PersonalDetails> PersonalDetails { get; set; }
public DbSet<HoroscopeDetails> HoroscopeDetails { get; set; }
public DbSet<Expectations> Expectations { get; set; }
public DbSet<FamilyDetails> FamilyDetails { get; set; }
public DbSet<AddressDetails> AddressDetails { get; set; }
public DbSet<EducationalDetails> EducationalDetails { get; set; }
}
public class UserDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public List <PersonalDetails> PdetailsList { get; set; }
public List<HoroscopeDetails> HDetailsList { get; set; }
public List<EducationalDetails> EDetailsList { get; set; }
public List<AddressDetails> AdddetailsList { get; set; }
public List<FamilyDetails> FDetailsList { get; set; }
public List<Expectations> ExpDetailsList { get; set; }
public PersonalDetails Pdetails { get; set; }
public HoroscopeDetails HDetails { get; set; }
public EducationalDetails EDetails { get; set; }
public AddressDetails Adddetails { get; set; }
public FamilyDetails FDetails { get; set; }
public Expectations ExpDetails { get; set; }
public UserDetails()
{
Pdetails = new PersonalDetails();
HDetails = new HoroscopeDetails();
EDetails = new EducationalDetails();
Adddetails = new AddressDetails();
FDetails = new FamilyDetails();
ExpDetails = new Expectations();
}
}
public class PersonalDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 0)]
public int PersonalID { get; set; }
public int UserId { get; set; }
public string RegID { get; set; }
public string FirstName { get; set; }
public string MidName { get; set; }
public string LastName { get; set; }
}
public class HoroscopeDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Rashi { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class EducationalDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 0)]
public int EduID { get; set; }
public string EducationDetails { get; set; }
public string Education { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class AddressDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddId { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class FamilyDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int FId { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class Expectations
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ExpId { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
现在来自控制器我想更新记录
public void UpdateRecord(FormCollection coll)
{
int id = Convert.ToInt32(Session["UserId"]);
UserDetails obj = db.UserDetails.FirstOrDefault(s => s.UserId.Equals(id));
obj.Pdetails.FirstName = "xxxx";
obj.Pdetails.LastName = "xxxx";
obj.UserId = Convert.ToInt32(Session["UserId"]);
db.Entry(obj).State = EntityState.Modified;
int result = db.SaveChanges();
}
它不会给出任何错误,但也不会更新任何记录
obj
对象还包含以下代码的空值:
UserDetails obj = db.UserDetails.FirstOrDefault(s => s.UserId.Equals(id));