Solved

LINQ to SQL assign value to class property

Posted on 2010-09-16
5
667 Views
Last Modified: 2013-11-11
Given the database schema below which is my linq dbml model

I need to assign a role to the user partial class.   So I want to say something like

user.rolename  = "temp Role"

I need to implement it like this.....



   public static User TempUser
        {
            get
            {
                return new User() { userName = "Temp",  RoleName = "temp role"  };
            }
        }

How can I do this?
USER
UserID           Int
RoleID           Int
Username         varchar(100)



ROLES
RoleID           int
RoleName         varchar(100)

Open in new window

0
Comment
Question by:ToString1
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33692991
Hi ToString1;

Don't understand the question, in the below statement User does not have a property name.

user.rolename  = "temp Role"

Do you want an object that contains all User properties as well as rolename returned from a query?

public class UserRole
{
    public int UserID            { get; set; }
    public int RoleID             { get; set; }
    public string Username   { get; set; }
    public string Rolename   { get; set; }
}

Fernando
0
 

Author Comment

by:ToString1
ID: 33693201
Hi

I am implementing custom principle object and want to assign a user a role of "temp role".  

The USER object is related to the ROLES object via roleID

if you look at the code snippet below, in other code I am calling the static property TempUser and assigning it a roleID but I want to check against the role name like this

 bool isAuthenticated = !(userRole.roleName == TempUser.RoleName);
public static User TempUser
        {
            get
            {
                return new User() { userName = "Temp", roleID = (int)UserRoles.TempRole};
            }
        }

 
        #region IPrincipal Members

        public IIdentity Identity
        {
            get
            {
                bool isAuthenticated = !(userRole.roleName == TempUser.userName);
                return new Identity(isAuthenticated, this.userName);
            }
        }

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33694048
May be this is what you are looking for.

bool isAuthenticated = !(userRole.roleName == TempUser.ROLES.RoleName);
0
 

Author Comment

by:ToString1
ID: 33695036
HI thanks

But if I try that I get "cannot convert type 'string' to Guest.Role
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33695737
I am not a ASP .Net developer so don't know much about authentication object hopefully someone else will drop in to help, sorry.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question