【发布时间】:2014-06-26 17:38:07
【问题描述】:
我正在尝试使用实体框架,但我的设计遇到了问题。我的 UserProfile 类有三个字段,UserId、UserName 和 jk_userHandle,前两个更新并且可以在我对对象进行更改时保存。但是我什至无法创建非空版本 jk_userHandle。我不确定我哪里出错了。
我已经 google/stackoverflow 到了地狱,并询问了我办公室的人,似乎没有人知道 -_-
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public void changeDataItem_edit(UserProfile choice, jk_userProfile model)
{
var found = UserProfiles.Find(choice.UserId); //finds prof
if (found == null) throw new KeyNotFoundException();
UserProfile tempProfile = choice;
tempProfile.jk_userHandle = model;
/*
stuff i found on stackoverflow, but
found.jk_userHandle = model;
or
found.jk_userHandle.biography = model.biography
don't work either. In all scenarios, when I step through i see my dbset's value change, but will not persist out of the http post. I've tried db.SaveChanges() (outside this function call) as well.
Stackoverflow link: http://stackoverflow.com/questions/7850774/updating-a-reference-to-a-child-object-in-entity-framework-4-1-codefirst
*/
this.Entry(found).CurrentValues.SetValues(tempProfile);
found.jk_userHandle = tempProfile.jk_userHandle;
this.SaveChanges();
}
}
这是 userprofile 类:
[Table("UserProfile")]
public class UserProfile
{
public jk_userProfile jk_userHandle; //is the handle for the rest of the account info...
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public UserProfile()
{
jk_userHandle = new jk_userProfile();
}
}
为了以防万一,这里是 jk_userHandle
public class jk_userProfile
{
public string userName { get; set; }
[DataType(DataType.Date)]
public string lastLogin { get; set; }
[DataType(DataType.Date)]
public string creationDate { get; set; }
[DataType(DataType.MultilineText)]
[Display(Name = "Just Connekt Biography", Description = "Write about yourself, favorite movies, shows, activities, what you like to do, etc...")]
public string biography { get; set; }
public jk_userProfile()
{
}
}
【问题讨论】:
标签: c# .net entity-framework