How to reference using Entity Framework and Asp.Net Mvc 2
- by Picflight
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,
[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)