views:

143

answers:

1

Supose I have 2 tables

usersTable

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

ArticleTable

  • Articleid int not null
  • Article ntext
  • userid guid not null
  • username nvarchar not null
  • ...more fields

To add an article, a user has be loged in. So I do this to get their userId and username

 ' get the user currently logged in
        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 article is inserted into the Article table by the logged in user, I want to add the username and userid to the article table. I am using Entity FrameWork 4.0 and programming in VB thanks yousaid

+1  A: 

You can override the SaveChanges method in your object context and then check for the Entities that are in the Insert State. So if you object context is called MembershipEntities and it's auto generated for you, you can create a partial class called MembershipEntities.

In C#...

public partial class MembershipEntities
{
    public override int SaveChanges(SaveOptions options)
    {
        var memberships = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Select(e => e.Entity).ToList();

        //Do what you need to do before you insert your data...

        int changes = base.SaveChanges(options);

    }
}

I have not tested this code, but it should work..

Dan H
Thanks Dan, but I already have the membership part working fine using Entity Framework. My question is how to add the username of the person adding an article to the Article table.That is, when the user logs in, their username is added to the article they are submitting. This works fine if I don't use Entity Framework.cheers,yousaid
Yousaid
I just used the Membership for my example so you can change it to be used how you need it. Now I'm confused because you say you have the username of the logged in user. That user is adding a new article. Do you have a property on your Article entity called username? If you have the username before you save the entity you just set the entities username property equal to the username of the logged in user and then save it.
Dan H
Thanks again. Yes I have a property in my Article Entity called username. The Article Entity looks like this •Articleid int not null •Article ntext •userid guid not null •username nvarchar not null •...more fields ' to get the user currently logged in: Dim currentUser As MembershipUser = Membership.GetUser() Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid) I have a Textbox named usernameTextbox. When the page loads, it captures the username. So how do I set this property to the value of the textbox before savechanges? Sorry I am still learning C#
Yousaid
I converted your c# code and it looks like this in VB. What do I need to add? Imports System Public Partial Class MembershipEntities Public Overrides Function SaveChanges(options As SaveOptions) As Integer Dim memberships = Me.ObjectStateManager.GetObjectStateEntries(EntityState.Added).[Select](Function(e) e.Entity).ToList() 'Do what you need to do before you insert your data... Dim changes As Integer = MyBase.SaveChanges(options) End Function End Class
Yousaid
Okay, so you have your page load that gets a MembershipUser object and you call it currentUser. It also gets a guid and sets currentUserId equal to that guid. So your currentUser should have a username property. Correct? Okay, so you have your logged in users username and the guid. Now you need to create your Article Entity and populate it with the article information and also set your Article.username = currentUser.username (or Article.username = textbox.Text) and Article.userid = currentUserId. Now you attach the Article entity to the objectContext and then call contextObject.SaveChanges().
Dan H