C# 使用另一个类添加数据库项

C# 使用另一个类添加数据库项,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,很抱歉标题不好,很难描述这个问题。在我的ASPX站点上,当用户单击按钮时,我需要生成两个新的数据库项。我首先需要创建我的用户对象,其中包含诸如电子邮件、密码和作为该表主键的UserID等内容。这已经运行了很长一段时间了,因为它非常简单,我甚至还使用外键设置了一些其他表来引用用户表(例如用户上传的图像的图像)。今天,我开始实现Profile类,当用户注册时,它将生成一个默认概要文件 profile类采用5个参数来生成: User user, string TagLine, string A

很抱歉标题不好,很难描述这个问题。在我的ASPX站点上,当用户单击按钮时,我需要生成两个新的数据库项。我首先需要创建我的用户对象,其中包含诸如电子邮件、密码和作为该表主键的UserID等内容。这已经运行了很长一段时间了,因为它非常简单,我甚至还使用外键设置了一些其他表来引用用户表(例如用户上传的图像的图像)。今天,我开始实现Profile类,当用户注册时,它将生成一个默认概要文件

profile类采用5个参数来生成:

 User user, 
 string TagLine,
 string AboutMe,
 string ProfilePhoto,
 string ProfilePhotoExtension
现在你不必担心最后4个,我遇到的问题是用户。它接受一个用户类,并充当用户表主键的外键(这样我就可以检索要放入配置文件的用户名等信息)

下面是负责生成新用户的代码片段,然后是基于此的概要文件:

            //Add user
            var newUser = new BO.User(txtEmail.Text, newHashedPassword, txtUsername.Text);
            //Create new profile
            var newProfile = new BO.Profile(newUser, "This user has not yet set their tagline", "This user has not yet set their about me", "default", ".jpg");
            db.UserAdd(newUser);
            db.ProfileAdd(newProfile);
            db.Save();
如您所见,我在newUser变量中生成了一个新的user类,然后我尝试使用该变量生成一个新的概要文件。我想这就是产生我的错误的原因。之后,我将它们添加并保存到我的数据库中

我得到的错误是:

System.Web.HttpUnhandledException(0x80004005):引发了类型为“System.Web.HttpUnhandledException”的异常。-->System.NullReferenceException:对象引用未设置为对象的实例。在c:\visualstudios\PiccyPic\PiccyPic.BO\Profile.cs中的PiccyPic.BO.Profile..ctor(用户用户、字符串标记行、字符串aboutme、字符串profilephoto、字符串profilephotoextension)处:piccypc.Login.butRegister\u单击(对象发送方、事件参数e)在c:\Visual Studio\PiccyPic\PiccyPic\Login.aspx.cs中:System.Web.UI.WebControls.Button.OnClick(EventArgs e)在System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)在System.Web.UI.WebControls.Button.System.Web.UI.iPosBackEventHandler.RaisePostBackEventEvent(String eventArgument)中的第134行System.Web.UI.Page.ProcessRequestMain中System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)中System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)中System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean IncludeStagesSafterAsyncPoint)中System.Web.UI.Page.HandleError的布尔IncludeStages(例外e)位于System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean IncludeStagesSafteraSyncpoint)的System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesBeforeAsyncPoint,Boolean IncludeStagesSafteraSyncpoint)的System.Web.UI.Page.ProcessRequestWithNoAssert的System(HttpContext上下文)位于System.Web.UI.Page.ProcessRequest(HttpContext上下文)位于ASP.login\u aspx.ProcessRequest(HttpContext上下文)在c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c03cf19f\4ceaa030\App_Web\u mnmhlw3a.14.cs中:System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IEExecutionStep.ExecuteStep(IEExecutionStep step,布尔值和同步完成)

以下是我收到错误时调试的屏幕截图:


如果您需要更多信息,请询问,我不确定什么对解决此问题有帮助。

如果您使用实体框架,构建此框架的通常方式是让
用户
类引用配置文件(因为配置文件属于用户):

然后,在创建用户后,可以设置配置文件

newUser.UserProfile = new BO.Profile("This user has not yet set their tagline", "This user has not yet set their about me", "default", ".jpg"); //note that we no longer pass a reference to the user

关于首先需要做什么,用户或数据库中的其他条目?哪些记录需要在另一条记录之前完成?另外,您是否从原始条目中获取主键?并将其映射到新数据?只有略读后,才会尝试找出发生了什么事。

看起来新用户对象在Pro中为空文件创建,因为配置文件的其他参数是已经存在的字符串。只需通过调试器查看哪个参数==null,并修复它们。感谢您的回答。如果我执行了上面的代码,该代码将如何在数据库中生成,我指的是如何链接用户配置文件记录?@Frayt将有两个表:
User
Profile
。您的
Profile
表将有一个名为
User\u Id
的列,并且应该创建一个外键,该外键将创建两个表之间的链接。很好,这就是我希望它生成的方式。我会尝试一下,然后返回给您。我的操作与您的代码相反,因为您的代码生成了一个de>Profile\u Id列在
User
上,所以我做了
public User-User
,然后是newProfile.User=newUser,这就产生了所需的结果。谢谢你的帮助!有点长,但是你有什么提示,为什么当我试图通过一个Profile实例引用User.Username时,用户是空的?这应该是一个注释,而不是回答我现在知道你是SO新手。我不确定当你没有足够的代表发表评论时,寻求进一步信息的程序是什么。好的,我现在尝试添加评论,这是我的第一个意图,但没有代表我不能发表评论。也许新来的人应该能够首先发表评论?这很有效。
newUser.UserProfile = new BO.Profile("This user has not yet set their tagline", "This user has not yet set their about me", "default", ".jpg"); //note that we no longer pass a reference to the user