如何在Entity Framework 4.0中使用插入事件将字段添加到相关表? [英] How do I use Inserting Event in Entity Framework 4.0 to add fields to a related table?

查看:67
本文介绍了如何在Entity Framework 4.0中使用插入事件将字段添加到相关表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Supose我有2张桌子b


usersTable

• userid guid not null

•用户名nvarchar

• ...更多字段



OrderTable

• Orderid int not null

• OrderTitle nvarchar

• userid guid not null //这是一个FK

• username nvarchar not null

• ...更多字段



要添加订单,用户必须因此我这样做是为了获得他们的userId和用户名

 '让用户当前登录。然后我可以将其绑定到文本框等。
         Dim currentUser As MembershipUser = Membership.GetUser()


        '确定当前登录用户的UserId值


        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey,Guid)



我想要的是,当一个 订单插入 ordertable 时登录用户,我想将用户名和用户ID添加到
ordertable。我正在使用Entity FrameWork 4.0,一个DetailsView 并在VB + VS2010中编程。 />
谢谢你

Supose I have 2 tables

usersTable
•userid guid not null
•username nvarchar
•...more fields

OrderTable
•Orderid int not null
•OrderTitle nvarchar
•userid guid not null //This is a FK
•username nvarchar not null
•...more fields

To add an order, a user has to be loged in. So I do this to get their userId and username
 ' get the user currently logged in. I can then bound it to Textbox etc
        Dim currentUser As MembershipUser = Membership.GetUser()
        ' Determine the currently logged on user's UserId value
        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)

What I want is, when an Order is inserted into the ordertable by the logged in user, I want to add the username and userid to the ordertable. I am using Entity FrameWork 4.0, a DetailsView and programming in VB + VS2010.
thanks yousaid

推荐答案

你好,

 

我们可以定义自己的业务逻辑来添加插入的Order实体的一些值。  
一般来说,我们在这里列出的业务逻辑中有几个要处理的事件,
http://msdn.microsoft.com/en-us/library/dd456876.aspx  

We can define our own business logic to add some values of the inserted Order entities.   Generally, we have several events to handle in our business logic which are listed here, http://msdn.microsoft.com/en-us/library/dd456876.aspx. 

 

我建议处理SavingChanges事件处理程序并使用ObjectStateManager.GetObjectStateEntries(EntityState.Added)来获取所有已添加
状态的实体。  对于这些添加的条目,我们可以继续检索所有插入的订单实体并设置其userId和用户名属性。  
详情请参阅
http://msdn.microsoft.com/en-us/library/cc716714.aspx   

I would recommend handle the SavingChanges event handler and use ObjectStateManager.GetObjectStateEntries(EntityState.Added) to get all the entities which are in Added status.  For these added entries, we can go on to retrieve all the inserted Order entities and set their userId and username properties.   For detail, please see http://msdn.microsoft.com/en-us/library/cc716714.aspx.  

 

如果您有任何疑问,请随时告诉我。

 

美好的一天!

 

 

最诚挚的问候,

Lingzhi Sun

MSDN订阅者支持
在论坛

如果您对我们的支持有任何反馈,请联系
msdnmg@microsoft.com

If you have any feedback on our support, please contact msdnmg@microsoft.com


这篇关于如何在Entity Framework 4.0中使用插入事件将字段添加到相关表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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