Solved

Updating Boolean field in MSSQL using LINQ and Entity Framework

Posted on 2011-02-28
6
538 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

776 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