【问题标题】:Why do I get “Cannot implicitly convert type 'int' to User” when trying to insert an INT in FOREIGN KEY using EF Core?尝试使用 EF Core 在 FOREIGN KEY 中插入 INT 时,为什么会出现“无法将类型 'int' 隐式转换为用户”?
【发布时间】:2021-04-10 15:08:48
【问题描述】:

我在 .NET Core Winforms 应用程序中使用 EF Core。我想插入一个 INT 作为外键,但出现错误

  namespace FTAS
    {
   public partial class SecurityQuestion : Form
        {
   
    public user users;
    
    
    public SecurityQuestion(LoginForm loginForm, user userdata)
    {
        InitializeComponent();
        this.users = userdata;
        
        
    }
   
    private void BtnSubmitQuestion_Click(object sender, EventArgs e)
    {

        if (IsValid())
        {
            var context = new FtasDbContext();

            context.SQuestion.Add(new Entity.SQuestions
            {
                Question = Questioncombo.Text,
                Answer = TxtQuestionAns.Text,
                User = users.Id

            }) ;

            context.SaveChanges();

        }

    }

   

    private bool IsValid()
    {
        if (Questioncombo.Text == string.Empty)
        {
            return false;
        }
        else
      if (TxtQuestionAns.Text.Trim() == string.Empty)
        {
            return false;
        }
        else
            return true;
    }

}
}

但这是表用户的核心INT

 namespace FTAS.Entity
 {
    [Table("User")]
   public class user
{
    public int Id { get; set; }


    public string Name { get; set; }


    public string FatherName { get; set; }


    public long Cnic { get; set; }


    public string Gender { get; set; }

    public long Mob { get; set; }

   


    public string Email { get; set; }


    public string Username { get; set; }


    public string Password { get; set; }


    public string Designation { get; set; }    


    public DateTime DateOfBirth { get; set; }

  
    
}
    
}

那为什么我得到错误 CS0029 无法将类型“int”隐式转换为“FTAS.Entity.user”

我也在尝试
私人用户 int32(int id) { 抛出新的 NotImplementedException(); } 和改变 用户 = int32(users.Id)

然后我得到错误

'方法或操作没有实现。'

Entity.SQuestion 的代码

  class SQuestions
  {
    public long Id { get; set; }

    public string Question { get; set; }


    public string Answer { get; set; }
   
    public user User { get; set; }

    
    }
}

【问题讨论】:

  • 能否为 Entity.SQuestions 添加源代码?问题可能就在那里。
  • 您得到“方法或操作未实现。”因为你的代码抛出了 NotImplementedException。
  • 您正在尝试将int 插入类型user。你的意思是user.Id = users.Id
  • @janzen User 是 SQuestion 的一个属性,即外键
  • @Russ 任何解决方案...?

标签: c# .net-core entity-framework-core


【解决方案1】:

类 SQuestions 中的属性 User 是一个类型“user”,它与 users.Id 不同类型,它是一个 int 类型。分配的类型必须相同。

变化:

User = users.Id

到:

User = users

【讨论】:

  • Microsoft.EntityFrameworkCore.DbUpdateException: '更新条目时出错。有关详细信息,请参阅内部异常。 内部异常 SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“用户”中插入标识列的显式值。然后我得到 context.SaveChanges(); 的异常
【解决方案2】:

在这段代码中 命名空间 FTAS.Entity { 类 SQuestions { 公共长 ID { 获取;放; }

    public string Question { get; set; }


    public string Answer { get; set; }
   
    public user User { get; set; }
    
   }
  }

改变

public user User { get; set; }

public user User { get; set; } 
[ForeignKey("User")]
public int UserFK { get; set; }

并解决了问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    相关资源
    最近更新 更多