2017-01-11 9 views
0

をidentitymodelするためにカスタムフィールドを追加し、私はMVCのアイデンティティモデルに新しいフィールドを追加した

public class ApplicationUser : IdentityUser 
{ 
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int AccountID { get; set; } 
    public int JeAdmin { get; set; } 
    public Ekipa Ekipa { get; set; } 
} 

マイekipaクラスには、次の要素で構成されます

私は私のオブジェクトを設定するにはどうすればよい
 public class Ekipa 
{ 
    public int id { get; set; } 
    public string Ime { get; set; } 
    public int LeaderID { get; set; } 
    public int Geslo { get; set; } 

} 

ログインしたユーザーにデータベースから取得しますか? 私は、現在のユーザーを取得し、

 ApplicationDbContext db = new ApplicationDbContext(); 
     var currentUserId = User.Identity.GetUserId(); 
     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
     var currentUser = manager.FindById(User.Identity.GetUserId()); 
     EkipaDb db1 = new EkipaDb(); 
     currentUser.Ekipa = db1.Ekipa.Where(m => m.id == 1); 

は、どのように私はidが1であるEkipaDbからオブジェクトを取得し、現在のユーザーにそれを追加するには?

答えて

0

オブジェクトをデータベーステーブルの列に追加することはできませんが、外部キーを追加することはできます。

追加:

public int EkipaId { get; set; } 

そしてEkipaオブジェクトを仮想します:

[ForeignKey("EkipaId")] 
public virtual Ekipa Ekipa { get; set; } 

これはApplicationUser表にEpikaオブジェクトのIdを保存すると、あなたが取得するために、ナビゲーションプロパティを使用することができますApplicationUserをロードするときにオブジェクト自体。

+0

ありがとうございます。私はそれが何とかMVCで可能だと思った。 :) – Ashley

+0

MVCよりもエンティティフレームワークが大好きです。 –

関連する問題