コード:
Something smt = new Something(){
Prop = 123,
Prop2 = "asdad"
}
foreach(var related in relatedsomething)
{
smt.Related.Add(new Related(){
relatedprop = 123,
};
}
ランタイムで null 参照に関するエラーが表示されます。関連はバーチャルアイコレクション。エンティティに外部キー フィールドが定義されていません。
逆に私がそうするなら
foreach(var related in relatedsomething)
{
db.Related.Add(new Related(){
relatedprop = 123,
Something = smt
};
}
できます。
ただし、最初のスニペットのように機能させたいです。
私は何か間違ったことをしていますか?出荷された EF4 では、両方の方法で機能します。
モデルクラス (関連部分):
public class Printer
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Replica> Replicas { get; set; }
}
public class Replica
{
public int Id { get; set; }
public virtual Printer Printer { get; set; }
}
public class PrintersContext: DbContext
{
public DbSet<Printer> Printers { get; set; }
public DbSet<Replica> Replicas { get; set; }
}