2011-11-10 7 views
5

外部(キー)オブジェクトをロードせずに別のオブジェクト(この例ではaspnet userId)を参照する新しいEFオブジェクトを作成します。参照オブジェクト全体を読み込まずに外部キー参照を使用して新しいEFオブジェクトを作成する

だから、基本的に、私は次の操作を実行したい:私は役立つだろう何かを見つけることができませんbs.aspnet_UsersReferenceで

buskerSet bs = new buskerSet(); 
bs.Title = title; 
bs.Image = image; 
bs.Created = DateTime.Now; 
//The following will crash, because bs.aspnet_Users is null 
bs.aspnet_Users.UserId = userId; 

context.SaveChanges(); 

..

SOLUTION:

aspnet_Users user = new aspnet_Users { UserId = userId }; 
context.AttachTo("aspnet_Users", user); 
set.aspnet_Users = user; 

答えて