Home > Software engineering >  Property Backer changes returning unknown error on Linq query
Property Backer changes returning unknown error on Linq query

Time:11-02

I have an application in ASPCore MVC.

My goal was to adjust the model to automatically capitalize email addresses. It seemed this would be better than adjusting each controller.

After some research, I came upon this solution below, to modify the property backers.

    public class PostFou
{
    private string _name;
    private string _email;
    private string _address;

    [Key]
    public int FouId { get; set; }
    [Display(Name = "Full Name")]
    [RegularExpression(@"[a-zA-Z0-9""'\s-|\.\=\ \*\/\\]*$")]
    [StringLength(45)]
    public string FouName { get; set; }

    [Display(Name = "Phone")]
    [RegularExpression(@"[0-9]*$")]
    [StringLength(20)]
    [DataType(DataType.PhoneNumber)]
    public string FouPhon { get; set; }

    [Display(Name = "Email")]
    [StringLength(45)]
    public string FouEmai //{ get; set; }
    {
        get { return _email; }
        set { _email = value.ToUpper(); }
    }
}

The problem is that now all my linq queries are returning a nullReference exception one things that seem unrelated. for example, in my razor view, I have the following code

@foreach (var item in Model) { //error occurs here
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FouZero)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FouName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FouPhon)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FouEmai)
        </td>

In the controller, for this view, there is a Linq simple query:

public IActionResult Index(string searchString)
    {

        var titles = _context.PostFous.Where(m =>
            m.FouZero.Contains(searchString) ||
            m.FouName.Contains(searchString) ||
            m.FouEmai.Contains(searchString)
            );

        //return View(await _context.PostFous.ToListAsync());
        return View(titles);
    }

in debugging, digging as deep as I know how, I found this message, consistently across the errors:

   at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )

I'm not sure what that means.

The question is, what is going wrong? Given that that is a broad question, more narrowly, in adjusting the property backing, should I have also adjusted something else? Follow up, is there a different way to accomplish my goal of auto-capitalizing the email addresses in the model?

I would like to avoid making changes to the db, and keep this within the application.

CodePudding user response:

My guess would be that the field could be null-able in the data. This means your setter needs to handle the fact that the DB row might have a #null eMail address, and your filter queries would as well:

[Display(Name = "Email")]
[StringLength(45)]
public string FouEmai //{ get; set; }
{
    get { return _email; }
    set 
    { 
        if (value == null)
            _email = null;
        else
            _email = value.ToUpper(); 
    }
}

Then when querying:

    var titles = _context.PostFous.Where(m =>
        m.FouZero.Contains(searchString) ||
        m.FouName.Contains(searchString) ||
        (m.FouEmai != null && m.FouEmai.Contains(searchString))
        );

For performance considerations I recommend defaulting text matching searches to default to BeginsWith() where users would be expected to enter what values start with rather than Contains. For instance If they type "mark" then they are typically looking for names/emails that start with "mark" but if they don't find a match and want a more open search looking for "%" or "*" common wildcard characters. That way they can opt to search for "mark" for a Contains. This gives them the flexibility while helping ensure that the majority of such queries can perform better.

CodePudding user response:

I tried again and reproduced the similar error:

enter image description here

enter image description here

In first case,the FouNum property of PostFou entity could not be null,

in db,the FouNum column could be null

And if the data in db was not sharing the same type with the property it would also get the similar error

You could check if any property type of int (Maybe FouNum in your case?) was in similar situation and share the more detailed information with us

  • Related