i wanna add and an entity and get its identity before savechanges() and set it as a foreign key of another entity before savechanges in entity framework code first. is it possible?
first model
public class A
{
int AId { get; set; }
string name { get; set; }
}
second model
public class B
{
int BId { get; set; }
int AId { get; set; }
string name { get; set; }
}
and…..
db.As.Add(A);
b.AId = A.Aid;
db.savechanges();
is there any article which explains how it works?
Declare a property of type
AinB.Then assign the instance of
Ato that navigational property. EF will determine the insert/update order of entities resolve the FKs.