使用实体框架

时间:2015-09-30 14:20:41

标签: c# asp.net sql-server asp.net-mvc

我有一个场景,我维护用户的个人资料。配置文件表包含20列,其中10列是必填字段。最初当用户注册时,我将该信息(名字,姓氏,国家,电子邮件,密码)保存在我项目的默认数据库中,然后我有一个单独的SQL数据库,我在其中为每个用户维护一个配置文件。我在注册阶段(如上所述)中的字段被输入到Sql数据库中,并将其余字段留空,以供用户稍后填写。

var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Country = model.Country };

var profile = new Profile { CanID = model.Email, Email = model.Email, FName = model.FirstName, LName = model.LastName, Country = model.Country ,ProfileStatus = "0", CreatedOn = DateTime.Now.ToShortDateString()};

var result = await UserManager.CreateAsync(user, model.Password);

 if (result.Succeeded)
 {
      db.Profiles.Add(profile); 
      db.SaveChanges();

      //This fails because a few required fields are left out so it has to fail that i understand.

     //Also the below code has to fail because since the record is not created than how can the attach function work. 

     db.Profiles.Attach(profile);
     db.Entry(profile).Property(p => p.CanID).IsModified = true;
     db.Entry(profile).Property(p => p.Email).IsModified = true;
     db.Entry(profile).Property(p => p.FName).IsModified = true;
     db.Entry(profile).Property(p => p.LName).IsModified = true;
     db.Entry(profile).Property(p => p.Country).IsModified = true;
     db.Entry(profile).Property(p => p.ProfileStatus).IsModified = true;
     db.Entry(profile).Property(p => p.CreatedOn).IsModified = true;
 }

var user保存到我项目的默认db中,var profile将保存在SQL数据库表中,其名称为Profile。

所以底线是,我有一个表配置文件,其中有20列,其中10个是必需的,当用户最初注册时,我将该信息保存在默认数据库中,我也想将其保存在Sql Db& #39;的个人资料表。那么如果在注册阶段没有填写所有必填字段,我将如何在Profile表中保存记录?它的创建方案更精确。

0 个答案:

没有答案
相关问题