更新EF ICollection属性

时间:2012-10-31 19:17:41

标签: asp.net-mvc entity-framework one-to-many

如何更新EF中的ICollection属性?

我收到此错误消息;

“无法将类型'int'隐式转换为'System.Collections.Generic.ICollection<MySolution.Domain.UserProfile>'”

我的实体:

public class Department
{
    public virtual int Id { get; set; }
    public virtual string DepartmentTitle { get; set; }
    public virtual ICollection<UserProfile> UserProfiles { get; set; }

}

public class UserProfile
{
    public virtual int UserId { get; set; }
    public virtual string UserName { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }

}

代码;

    var userProfile = _db.UserProfiles.Single(d => d.UserName == "SomeUserName");

    var department = _db.Departments.Single(d => d.Id == variableDepartmentID);
    department.UserProfiles = uProfile.UserId;
    _db.UpdateDepartment(department);
    _db.Save();

0 个答案:

没有答案