simba
simba

Reputation: 463

LINQ error on select

I am writing this LINQ to EF query to get a matching item.

if (user.UserID > 0)
{
    var TempUser = (from c in GSData.tblUsers
                    where c.UserID == user.UserID
                    select c).First();
                    ..........

The value of user.UserID at runtime is 579 and there is a matching row. However i get the error

{"This property cannot be set to a null value."}
System.Data.ConstraintException was unhandled by user code

Further it breaks surprisingly at place where middle name is set. It happens to be a field in tblUsers database with nvarchar(20) and the value is null.

 _MiddleName = StructuralObject.SetValidValue(value, false);

However i had written similar queries for getting matching items and they work fine. I am just selecting and not updating any values. So i have been breaking my head over why this is happening. Any suggestions are welcome.

Upvotes: 0

Views: 397

Answers (1)

Cam Bruce
Cam Bruce

Reputation: 5689

Delete the table in your entity model, then choose update from database. EF won't pick up nullable/non-nullable changes just by updating an already existing table.

Upvotes: 1

Related Questions