如何在其他表中保存当前用户的UserName?

时间:2014-10-25 20:48:26

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 ef-code-first

我在Asp.Net MVC 4中开发,我有UserProfile表,其中包含以下字段:

        public class UserProfile
        {
            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int UserId { get; set; }
            public string UserName { get; set; }
            public string UserType { get; set; } //flag de tipo usuário musico ou ouvinte  
        }

我的应用很简单,只有将音乐上传到数据库,每个用户只能上传你的音乐。 在音乐领域我有这些领域:

    public class Musica
{
    public int MusicaId { get; set; }
    public int GeneroId { get; set; } //GenreID
    public int UserId { get; set; } //Needs to take the username from current user logged in app
    public string Nome { get; set; }
    public string NomeArtista { get; set; }
    public Genero genero { get; set; }
    public decimal Preco { get; set; }
}

这是我在SQL上的表关系:

CREATE TABLE [dbo].[Musicas] (
    [MusicaId]  INT            IDENTITY (1, 1) NOT NULL,
    [GeneroId]  INT             NOT NULL,
    [UserId]  INT               NOT NULL,
    [Nome]      NVARCHAR (MAX)  NOT NULL,
    [Preco]     MONEY           NULL,
    [NomeArtista] NVARCHAR(MAX) NOT NULL,   
    CONSTRAINT [PK_dbo.Musicas] PRIMARY KEY CLUSTERED ([MusicaId] ASC), 
    CONSTRAINT [FK_Musicas_ToTable] FOREIGN KEY ([UserId]) REFERENCES [UserProfile]([UserId])

当用户时,会在视图i want to take the UserName from User and put on field UserName at Music table创建您的音乐,我该怎么做?

我认为这样做很简单,但我不知道如何从用户那里获取UserName并保存在其他表中,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

string currentUserName = System.Web.HttpContext.Current.User.Identity.Name;

获取有关用户的一些信息......

MembershipUser user = Membership.GetUser(currentUserName...);
//    ....

...任选

MembershipUser user = Membership.GetUser();
string userName = user.UserName;