如何获取实体的ID并将其用于更新实体. [英] How to retrieve an entitie's ID and use it for updating the entity.

查看:72
本文介绍了如何获取实体的ID并将其用于更新实体.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我坚持认为,这是一个非常简单的问题.

我正在尝试将员工的登录时间,登录日期和注销时间保存到数据库中.到目前为止,我正在做的是,当员工首次登录时,它会保存他们的登录时间和日期,但显然不会保存他们的注销时间,因为他们尚未注销.接下来,当主窗体关闭时,它需要记录其注销时间并更新其现有记录以完成该操作.

好吧,到目前为止,我的是:

它将正确记录登录时间和登录日期.

并且它将正确记录注销时间,并且我具有将其记录到现有记录中所需的代码.

但是,我的主要问题是如何首先备份现有记录?

在创建新的登录记录时,我尝试使用一个变量来保存LoginDataID,以便在关闭时可以将其传递到主窗体上以通过其ID来显示LoginDataRecord.这部分,我似乎无法上班.

基本上,我要问的是有人会帮助我找到一种方法来捕获员工首次登录时创建的新LoginDataRecord的ID,以便在我使用LINQ查询LoginDataRecord时可以使用它来查找他们的LoginDataRecord并在主窗体关闭时用注销时间更新它?

同样在此刻,我严格来说是Visual Basic程序员.我仍在上大学,并且我使用Java和C#的技能非常非常基础,我在其中做不到很多.

无论如何,感谢您的所有帮助.

这是登录表单的代码.

Hello,

I''m stuck with what I''m sure, is a very simple problem.

I''m trying to save an employee''s login time, login date, and logout time into a database. So far what I''m doing is, when the employee first logs in it saves their login time and date, but obviously not their logout time since they haven''t logged out yet. Next when the main form is closing, it needs to record their logout time and update their existing record to complete it.

Okay so what I have so far is:

It will record the login time and login date correctly.

And it will record the logout time correctly and I have the code necessary to record it into an existing record.

But, my main problem is how do I pull back up that existing record in the first place?

While creating the new login record I tried to use a variable to save the LoginDataID so it can be passed onto the main form to bring the LoginDataRecord up by its ID when closing. This part, I can''t seem to get to work.

Basically, what I''m asking is will anyone help me find a way to capture the ID of the new LoginDataRecord that is created when the employee first logs in, so it can be used when I query the LoginDataRecord, using LINQ, to find their LoginDataRecord and update it with the logout time when the main form is closing?

Also at the moment, I''m strictly a Visual Basic programmer. I''m still in college, and my skills with Java and C# are very, very basic and I can''t really do much in them.

Anyways, thanks for any and all help.

Here''s the code for the Login Form.

Imports NEIPurchasingDAL.PurchasingDatabaseEntities
Imports System.Data.Objects

Public Class LoginForm

    Private PE As NEIPurchasingDAL.PurchasingDatabaseEntities = New NEIPurchasingDAL.PurchasingDatabaseEntities
    Public LoginID As String
     
    Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click

        Dim MyUser As String = My.User.Name
        Dim LoginDate As String = Today.ToShortDateString
        Dim LoginTime As String = Now.ToLongTimeString

        Dim MyUserName As String = Replace(MyUser, "NEIHERRIN\", "")

        MyUserName = My.User.Name

        Dim LoginDataRecord As New NEIPurchasingDAL.LoginData

        With LoginDataRecord
            .Username = MyUserName
            .LoginTime = LoginTime
            .LoginDate = LoginDate
        End With

        PE.AddToLoginDatas(LoginDataRecord)
	
        LoginID = LoginDataRecord.LoginID

        PE.SaveChanges()

        
        MainForm.Show()
        Me.Visible = False


    End Sub
   
End Class



这是主表单的代码



And here''s the code for the Main Form

Imports NEIPurchasingDAL.PurchasingDatabaseEntities
Imports System.Data.Objects

Public Class MainForm

    Private PE As NEIPurchasingDAL.PurchasingDatabaseEntities = New NEIPurchasingDAL.PurchasingDatabaseEntities
    Private MyUser As String = My.User.Name

    Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles_ Me.FormClosing

        Dim LogoutTime As String = Now.ToLongTimeString

        Try
            Dim LoginData As NEIPurchasingDAL.LoginData

            LoginData = (From LoginDatas In PE.LoginDatas
                         Where LoginDatas.LoginID = LoginForm.LoginID
                        Select LoginDatas)


            LoginData.LogoutTime = LogoutTime

            PE.SaveChanges()

        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try

        LoginForm.Visible = True

    End Sub
End Class



问题是我无法获取公共变量LoginID来将LoginID存储在实际实体上,然后将其用于Main Form上的比较.假设有可能.



The problem is that I can''t get the Public variable LoginID to store the LoginID on the actual entity and then use it for comparison on the Main Form. Assuming it is possible.

推荐答案

LoginID = LoginDataRecord.LoginID
PE.SaveChanges()



反转这些线.在使用SubmitChanges方法更新数据库之前,不会设置ID.



Reverse these lines. The ID will not be set until the database has been updated by the SubmitChanges method.


在您执行SubmitChanges()或SaveChanges()之后,您只需引用ID字段即可.

LoginDataRecord.LoginID

这将检索您的ID,但是您必须在进行更改后引用它.
AFTER you do a SubmitChanges() or SaveChanges(), you can just reference the ID field.

LoginDataRecord.LoginID

And that will retrieve your ID, but you have to reference this after the changes have been made.


我会设计数据库表,因此userid是表上的主键.具有自动编号的复合键可以使用.这样,为特定用户检索数据并进行适当更新将更加容易.登录时添加一条记录,注销时检索为用户输入的最后一条记录并进行更新.

您最擅长的是哪种语言,而不仅仅是逻辑和体系结构.
I would design the database tables so the userid is a primary key on the table. A composite key with an autonumber would work. This way it will be much easier to retrieve data for a particular user and update appropriately. At login you add a record, at logout you retrieve the last record entered for the user and update it.

It''s not a matter of which language you are best at it''s simply the logic and architecture.


这篇关于如何获取实体的ID并将其用于更新实体.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆