1

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".

1
  • Why have you got idUser in both Computer and User? I imagine you only need it in User, you can access it with c.User.idUser... and you already seem to know how to allow nulls based on how you used it in Computer Commented Apr 15, 2014 at 15:31

2 Answers 2

3

Mark idUser as nullable.

public int? idUser { get; set; }

Additionally you could get rid of of the idUser property and just use the User navigation prop. Since User is marked virtual you can always access the UserId with user.Id.

This is how I would set up your Computer model:

public class Computer
{
    public int Id { get; set; }
    public string ComputerName { get;set; }
    public virtual User User { get; set; }
}

Or:

public class Computer
{
    public int Id { get; set; }
    public string ComputerName { get; set; }
    public int? UserId { get; set; }
    public User User { get; set; }
}

Using my first example you could insert like this:

PDbContext _db = new PDbContext();
Computer c = new Computer();
c.ComputerName = "sdsd";
c.User = null;
_db.Computers.Add(c);
_db.SaveChanges();

And with my second:

PDbContext _db = new PDbContext();
Computer c = new Computer();
c.ComputerName = "sdsd";
c.UserId = null;
_db.Computers.Add(c);
_db.SaveChanges();
Sign up to request clarification or add additional context in comments.

7 Comments

sorry that was a typo, but the question still not be resolved.
I don't follow. What exactly is the question? You're trying to create a new Computer model with a null user?
When I run that Iget L'instruction INSERT est en conflit avec la contrainte FOREIGN KEY "FK_dbo.Poste_dbo.User_UserID".
UserId can't be take null but 0.
UserId can take a null if you mark it nullable. You can mark it nullable by adding the '?' after int. For example: public int? UserId { get; set; } ...
|
0
[ForeignKey("User")]
public int? idUser{ get; set; }

This is part of your Computer class. Since not every Computer has a User, you've used a nullable-int so that you can use null to represent the case where there is no User.

However, then, you do this:

c.idUser = 0;

This isn't correct. You were using a nullable-int for a reason - you should be setting idUser to null here. Instead, you're using 0 as the value of your foreign key. Since there is no row in your users table with a UserID of 0, this fails with the error you're seeing.

Change to this, and you'll get a null in the UserID column of the new row; because it's a null, the foreign-key constraint won't apply:

c.idUser = null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.