0
エンティティフレームワークコードの最初のアプリケーションの3番目のナビゲーションプロパティにデータを送信する際に問題が発生しています。エンティティフレームワークでコードを先に多数保存する
私のモデルは、このようなものです:(ユーザーと当社と呼ばれるモデルがあると仮定)
public enum UserCompanyRoleType
{
Administrator,
Creator,
Follower
}
/// <summary>
/// User that belongs to a company
/// </summary>
public class UserCompany
{
public int UserCompanyId { get; set; }
public virtual User User { get; set; }
public virtual int UserId { get; set; }
public virtual Company Company { get; set; }
public virtual int CompanyId { get; set; }
public virtual IEnumerable<UserCompanyRole> Roles { get; set; }
}
public class UserCompanyRole
{
public virtual int UserCompanyRoleId { get; set; }
public UserCompanyRoleType RoleType { get; set; }
public virtual UserCompany UserCompany { get; set; }
public virtual int UserCompanyId { get; set; }
}
ここでは、サービスコードです:
var creator = new UserCompany { UserId = userId };
//add the user as both creator and admin
creator.Roles = new List<UserCompanyRole> { new UserCompanyRole{RoleType = UserCompanyRoleType.Creator}}; //somehow this does not reach the database
company.Users = new List<UserCompany>();
company.Users.Add(creator);
db.Companies.Add(company);
db.SaveChanges();
これは、企業や関連usercompanyの両方を節約します2つの役割は保存されません。コメントした行に2つの役割を保存するにはどうすればよいですか?
ICollectionがIEnumerableで動作するのはなぜですか?私はすべてのコードをそれに変更すべきですか? –
@Lol [なぜ、エンティティフレームワークが遅延読み込みのためにICollectionを必要とするのですか?](http://stackoverflow.com/questions/2866881/why-does-the-entity-framework-need-an-icollection-for-lazy-ローディング) – Eranga