Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在一对一关系表中插入新记录_C#_Asp.net Mvc_Sql Server 2008_Entity Framework - Fatal编程技术网

C# 在一对一关系表中插入新记录

C# 在一对一关系表中插入新记录,c#,asp.net-mvc,sql-server-2008,entity-framework,C#,Asp.net Mvc,Sql Server 2008,Entity Framework,我有两个表供用户使用,一个用于保存用户公共信息,另一个用于保存额外信息,它们与UserId字段有一对一的关系 用户: UserId >> Primarykey auto increment UserName Password UserId >> Primarykey MoreInfo 用户信息: UserId >> Primarykey auto increment UserName Password UserId >>

我有两个表供用户使用,一个用于保存用户公共信息,另一个用于保存额外信息,它们与UserId字段有一对一的关系

用户:

UserId >> Primarykey auto increment  
UserName  
Password
UserId >> Primarykey  
MoreInfo
用户信息:

UserId >> Primarykey auto increment  
UserName  
Password
UserId >> Primarykey  
MoreInfo
在我的应用程序中,当用户注册一个新帐户时,我们在Users表中插入一行,然后如果他想更改一些信息,我们需要添加到UsersInfo表中

我面临的问题是,当我尝试在UsersInfo表中插入时,它总是告诉我UserId不能为null。在调用saveChanges之前设置了这个用户标识

var info = new UserInfo { UserId = userId, MoreInfo = text };
context.UserInfo.Add(info);

我不知道问题是在EF还是在db中,所以任何需要检查的信息或东西都会有帮助。

首先,你不需要使用两个单独的表,除非每个用户都有多组额外信息。如果您使用单个表,那么一个简单的更新查询将使事情变得简单

如果您想按自己的方式去做,首先测试userId是否为空或是否有一些值。如果它为null或空,则您一定会得到您提到的错误


看看是否有用。

首先,您不需要使用两个单独的表,除非每个用户都有多组额外信息。如果您使用单个表,那么一个简单的更新查询将使事情变得简单

如果您想按自己的方式去做,首先测试userId是否为空或是否有一些值。如果它为null或空,则您一定会得到您提到的错误


看看是否有帮助。

这不是解决方案,而是解决办法。 它可能会起作用:

Users:
----
UserId >> Primary key & Auto increment
UserName
Password

UsersInfo:
----
UsersInfoId >> Primary key auto increment
UserId >> Foreign key & Unique constraint (for one-to-one relation)
MoreInfo

这不是解决方案,而是解决办法。 它可能会起作用:

Users:
----
UserId >> Primary key & Auto increment
UserName
Password

UsersInfo:
----
UsersInfoId >> Primary key auto increment
UserId >> Foreign key & Unique constraint (for one-to-one relation)
MoreInfo