How to insert a Computer with an idUser as a new value?
public class User
{
public User()
{
}
[Key]
public int idUser; { get; set; }
[Required]
public string UserName { get; set; }
}
public class Computer
{
public Computer()
{
}
[Key]
public int idComputer{ get; set; }
public string ComputerName {get;set;}
[ForeignKey("User")]
public int? idUser{ get; set; }
public virtual User User { get; set; }
}
I've tried this way:
PDbContext _db = new PDbContext();
Computer c = new Computer();
c.ComputerName = "sdsd";
c.User = null;
c.idUser = 0;
_db.Computers.Add(c);
_db.SaveChanges();
But this wouldn't work. Any brilliant suggestion please?
When I run that I get L'instruction INSERT est en conflit avec la contrainte FOREIGN KEY "FK_dbo.Poste_dbo.User_UserID".
idUserin bothComputerandUser? I imagine you only need it inUser, you can access it withc.User.idUser... and you already seem to know how to allow nulls based on how you used it inComputer