使用另一个类添加数据库条目

时间:2014-11-18 18:37:47

标签: c# asp.net entity-framework

对于糟糕的头衔而言,很难描述这个问题。在我的ASPX站点上,当用户单击该按钮时,我需要生成两个新的数据库项。我首先需要创建我的User对象,其中包含诸如电子邮件,密码和UserID之类的内容,UserID是该表的主键。这已经很好地工作了很长时间,因为它非常简单,我甚至设置了一些其他表,其中外键引用了User表(例如用户上传的Images for Images)。今天我已经开始实现Profile类,它将在用户注册时生成默认配置文件。

profile类需要5个参数来生成:

 User user, 
 string TagLine,
 string AboutMe,
 string ProfilePhoto,
 string ProfilePhotoExtension

现在您不必担心其中的最后4个,我遇到问题的是用户用户。这需要一个用户类,并充当User表的主键的外键(这样我就可以检索诸如用户名之类的信息放入配置文件中。)

以下是负责生成新用户的代码片段,然后是基于该代码的配置文件:

            //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变量中生成一个新的用户类,然后我尝试使用该变量来生成一个新的配置文件。我认为这是产生我的错误的一点。之后,我将它们添加并保存到我的数据库中。

我得到的错误是:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object. at PiccyPic.BO.Profile..ctor(User user, String tagline, String aboutme, String profilephoto, String profilephotoextension) in c:\Visual Studios\PiccyPic\PiccyPic.BO\Profile.cs:line 37 at PiccyPic.Login.butRegister_Click(Object sender, EventArgs e) in c:\Visual Studios\PiccyPic\PiccyPic\Login.aspx.cs:line 134 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.login_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c03cf19f\4ceaa030\App_Web_mnmhlw3a.14.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

这是我收到错误时的调试截图:

screenshot of the debugging up to the point of when I get the error

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

2 个答案:

答案 0 :(得分:2)

如果您正在使用实体框架,通常的结构方式是User类可以引用配置文件(因为配置文件属于用户):

public Profile UserProfile {get;set;} 

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

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

答案 1 :(得分:-1)

就需要先做什么而言,用户或其他条目进入数据库?哪一条记录需要在另一条记录之前完成?另外,您是从原始条目中获取主键吗?并将其映射到新数据?只有撇去才能尝试找出正在发生的事情。