Solved

Updating Boolean field in MSSQL using LINQ and Entity Framework

Posted on 2011-02-28
6
539 Views
Last Modified: 2012-05-11
Hello All,

I'm having a really frustrating time updating any fields in my database that are of type Boolean!!!

For instance I have:
ClassManagementEntities db = new ClassManagementEntities();
UserInformation user = new UserInformation();
user.Email = model.Email;
user.Roles = "Student";
user.Username = model.UserName;
user.UserInformationId = (Guid)memUser.ProviderUserKey;
user.IsApproved = true;
db.UserInformation.Add(user);
db.SaveChanges();

Open in new window


I then check in SQL Server Mangaement Studio and the value for IsApproved is set to 0. So frustrating. I've run out of ideas. I feel like this should be a simple task... does the type need to be bool vs Boolean?
0
Comment
Question by:LaBowski
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 35004171
No, it should be just as easy as you figured.  I have boolean columns (of tyep Boolean) in virtually every table, and it's a simple to update as any other, and just as you have.

Any chance you have triggers or code somewhere else in your model that might be resetting the value?
Is the property mapped properly?
0
 

Author Comment

by:LaBowski
ID: 35004220
no triggers I don't believe. I don't even know how to set tiggers :\

I'm using the entity framework (CTP 5). I define the UserInformation class as:
public class UserInformation
    {
        public string Username { get; set; }
        public Boolean IsApproved { get; set; }
        public string Email { get; set; }
        public Guid UserInformationId { get; set; }
        public string Roles { get; set; }
        public string SecretQuestion { get; set; }
        public string SecretAnswer { get; set; }
        public string ReturnUrl { get; set; }
        public int SemesterID { get; set; }
        public virtual Grade Grade { get; set; }
        public virtual Absent Absent { get; set; }
    }

Open in new window


I really appreciate your help!
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35004265
instead of assigning value true, did you check with 1?
ClassManagementEntities db = new ClassManagementEntities();
UserInformation user = new UserInformation();
user.Email = model.Email;
user.Roles = "Student";
user.Username = model.UserName;
user.UserInformationId = (Guid)memUser.ProviderUserKey;
user.IsApproved = 1;
db.UserInformation.Add(user);
db.SaveChanges();

Open in new window

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:LaBowski
ID: 35004292
Weird... so now it's working properly... weird... Problem now is that the public string[] Roles { get; set;} is not being added to the database... so I have:
public class UserInformation
    {
        public string Username { get; set; }
        public Boolean IsApproved { get; set; }
        public string Email { get; set; }
        public Guid UserInformationId { get; set; }
        public string[] Roles { get; set; }
        public string SecretQuestion { get; set; }
        public string SecretAnswer { get; set; }
        public string ReturnUrl { get; set; }
        public int SemesterID { get; set; }
        public virtual Grade Grade { get; set; }
        public virtual Absent Absent { get; set; }
    }

Open in new window


I think cause it referencing Roles as Web.Security.Roles... which is unfortunate because I want to query users who have the role student from this table... do I need a public virtual Roles Roles get set or???
0
 

Author Comment

by:LaBowski
ID: 35004311
To Clarify in:

UserInformation user = new UserInformation();
                    user.Email = model.Email;
                    user.Roles = new string[] { "Student" };
                    user.Username = model.UserName;
                    user.UserInformationId = (Guid)memUser.ProviderUserKey;
                    user.IsApproved = true;

                    ProfileInformation profile = ProfileInformation.GetProfile(model.UserName);
                    profile.SemesterID = currentSemesterID;
                    user.SemesterID = Int32.Parse(currentSemesterID);
                    db.UserInformation.Add(user);
                    db.SaveChanges();

Open in new window


The line:
user.Roles = new string[] { "Student" };

Open in new window


Is not represented in my database.... as in it's not being save AND it's not even a column of the Userinformation Table...
0
 

Author Comment

by:LaBowski
ID: 35004330
I guess since I have the boolean part working I could create a field isStudent and set it to true if they're a student... but then I'll have to update another method when the user is set to faculty or admin isStudent is set to false... which is doable, but is it best practice... I don't think so... sorry just thinking out loud...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

821 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