简体   繁体   English

插入具有整数空值的行

[英]Insertion of a row with integer null value

How to insert a Computer with an idUser as a new value? 如何插入带有idUser的计算机作为新值?

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". 当我运行时,我得到L'instruction INSERT est en conflit avec la contrainte FOREIGN KEY "FK_dbo.Poste_dbo.User_UserID".

Mark idUser as nullable. 将idUser标记为可为空。

public int? idUser { get; set; }

Additionally you could get rid of of the idUser property and just use the User navigation prop. 此外,您可以摆脱idUser属性,只需使用用户导航道具。 Since User is marked virtual you can always access the UserId with user.Id. 由于User被标记为虚拟,因此您始终可以使用user.Id访问UserId。

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();
[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. 由于并非每台计算机都有用户,因此您使用了nullable-int,以便可以使用null来表示没有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. 您出于某种原因使用了nullable-int - 您应该在此处将idUser设置为null。 Instead, you're using 0 as the value of your foreign key. 相反,您使用0作为外键的值。 Since there is no row in your users table with a UserID of 0, this fails with the error you're seeing. 由于UserID为0的用户表中没有行,因此失败并显示您看到的错误。

Change to this, and you'll get a null in the UserID column of the new row; 更改为此,您将在新行的UserID列中获得null; because it's a null, the foreign-key constraint won't apply: 因为它是null,外键约束将不适用:

c.idUser = null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM