日期:2014-05-20  浏览次数:21067 次

问:用linq还要怎么处理?
INSERT 语句与 FOREIGN KEY 约束"FK_PersonalInfo_aspnet_Users"冲突。该冲突发生于数据库"FPDUsers",表"dbo.aspnet_Users", column 'UserId'。
语句已终止。
  插入按钮下:
 FPD.Users.PersonalInfo model = new PersonalInfo();

  //获取userid
  FPD.Users.aspnet_Users user = new aspnet_Users();
  model.UserID = user.UserId;

  model.Name = this.txtName.Text.Trim();
  model.Sex = this.rdolstSex.Text.Trim();
  model.Birth = Convert.ToDateTime(this.txtBirthday.Text.Trim());
  model.Birth = DateTime.Now;
  model.CodeType = this.ddlCertificateType.Text.Trim();
  model.Code = this.txtCertificateNum.Text.Trim();
  model.Marry = this.ddlMarryState.Text.Trim();
  model.Mobile = this.txtPhone.Text.Trim();
  model.High = this.txtStature.Text.Trim();
  model.Edu = this.ddlEducation.Text.Trim();
  model.Subject = this.ddlSpecialty.Text.Trim();
  model.Computer = this.ddlComputerLevel.Text.Trim();
  model.Loc_Now = this.ddlRegisterTown.Text.Trim();
  model.Work_Place1 = this.ddlCurrentPlace.Text.Trim();
  model.Work_Year = Convert.ToInt32(txtWorkYears.Text.Trim());  
  model.PostCode = this.txtPostCode.Text.Trim();
  model.Work_Live = this.TextJob.Text.Trim();
  model.Self_Remark = this.TextValue.Text.Trim();
  model.Specialty1 = this.ddlQZzhuanye1.Text.Trim();
  model.Specialty2 = this.ddlQZzhuanye2.Text.Trim();
  model.Apply_Type = this.ddlQZleixing.Text.Trim();
  model.Work_Date = this.ddlHSshangban.Text.Trim();
  model.Nation = this.ddljiguan.Text.Trim();
  model.Politics_title = this.ddlZZmianmao.Text.Trim();
  FPD.Users.BLL.InsertPersonalInfo(model);

BLL: public static bool InsertPersonalInfo(PersonalInfo model)
  {
  return dal.InsertPersonalInfo(model);
  }

DAL: public bool InsertPersonalInfo(PersonalInfo user) 
  {
  try
  {
  using (var dc = DataContext)
  {
  dc.PersonalInfo.InsertOnSubmit(user);
  dc.SubmitChanges();
  return true;//
  }
  }
  catch //报异常----INSERT 语句与 FOREIGN KEY 约束"FK_PersonalInfo_aspnet_Users"冲突。最前面一段
  {
  return false;
  }
  }
Userid 是 aspnet_Users 表的主键,是PersonalInfo的外键,插入的时候,先要将数据保存到aspnet_Users的userid里面去,然后才能根据userid传入PersonalInfo !!
问:用linq还要怎么处理?

------解决方案--------------------
这个你LINQ没关系。

你的FPD.Users.aspnet_Users 实体类有没有FPD.Users.PersonalInfo这个属性呢?
 如果有的话,那应该直接:

FPD.Users.PersonalInfo model = new PersonalInfo(); 

FPD.Users.aspnet_Users user = new aspnet_Users(); 
//model.UserID = user.UserId; 
user.PersonalInfo=model;//关联起来。

然后保存就应该就可以。
------解决方案--------------------
数据数据库,在SQL中转化日期格式
------解决方案--------------------
CONVERT函数:
比如:
SELECT CONVERT(varchar(100), GETDATE(), 25) 返回的是: 2010-01-29 13:10:47.250

所以你按照那样转换一下就可以了。
sql:
insert into table(xxx,xxx...) values (xxx,xxx,CONVERT(varchar(大小), 时间值, 25)..);

又或者使用C#代码转换到毫秒,在操作。比如:

Convert.ToDateTime("1949-10-01").ToString("yyyy-MM-dd HH:mm:ss.fff")