开发者

How to reference using Entity Framework and Asp.Net Mvc 2

Tables

CREATE TABLE [dbo].[Users](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Email] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[开发者_Python百科BirthDate] [smalldatetime] NULL,
[CountryId] [int] NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
([UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[TeamMember](
[UserId] [int] NOT NULL,
[TeamMemberUserId] [int] NOT NULL,
[CreateDate] [smalldatetime] NOT NULL CONSTRAINT [DF_TeamMember_CreateDate]  
  DEFAULT (getdate()),
CONSTRAINT [PK_TeamMember] PRIMARY KEY CLUSTERED 
([UserId] ASC,
[TeamMemberUserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

dbo.TeamMember has both UserId and TeamMemberUserId as the index key.

My goal is to show a list of Users on my View. In the list I want to flag, or highlight the Users that are Team Members of the LoggedIn user.

My ViewModel

public class UserViewModel
{
 public int UserId { get; private set; }
 public string UserName { get; private set; }
 public bool HighLight { get; private set; }

 public UserViewModel(Users users, bool highlight)
 {
  this.UserId = users.UserId;
  this.UserName = users.UserName;
  this.HighLight = highlight;
 }
}

View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MvcPaging.IPagedList<MyProject.Mvc.Models.UserViewModel>>" %>
<% foreach (var item in Model) { %>
 <%= item.UserId %>
 <%= item.UserName %>
 <%if (item.HighLight)  { %>
  Team Member
 <% } else { %>
  Not Team Member
<% } %>

How do I toggle the TeamMember or Not If I add dbo.TeamMember to the EDM, there are no relationships on this table, how will I wire it to Users object?

So I am comparing the LoggedIn UserId with this list(SELECT TeamMemberUserId FROM TeamMember WHERE UserId = @LoggedInUserId)

EDIT

TeamMemberUserId is the UserId of the User that is a team member.

I named the table Users to avoid confusion with the System.We.Security User class.


I presume TeamMemberUserId is really the TeamId?

Why is there no relationship between the tables?

Why is pluralization messed up? User vs Users?

Sounds like you need something like:-

  int userId = ... user Id for current user

  var teams = context.TeamMember.Where(tm => tm.UserId == userId);

  foreach (User other in ...)
  {
      int otherId = other.UserId;

       bool highlight = teams.Any(team => context.TeamMember.Any(tm => tm.TeamId == team.TeamId && tm.UserId == otherId ));
       ...
   }

Which would be much cleaner if you had the FK relationship in there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜