【发布时间】: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