Data is Null. This method or property cannot be called on Null values

后端 未结 7 993
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 20:07

I\'m working on an application where one can get information on movies from a database as well as add, update and delete the movies. In the database I have three tables (Movie,

相关标签:
7条回答
  • 2021-02-06 20:43

    This error happens immediately after I enabled C# 8 nullable feature in my Entity Framework Core 3.1 project.

    The solution is to change your entity properties to their nullable counterparts. For example,

    Change from:

    public class Person {
      public int Id { get; set; }
      public string Name { get;set; }
      public string Address { get;set; }
    }
    
    

    To:

    public class Person {
      public int Id { get; set; }
      public string Name { get;set; }
      public string? Address { get;set; }  //change address to nullable string since it is nullable in database
    }
    
    
    0 讨论(0)
  • 2021-02-06 20:47

    Edit your select statement as follows to handle null issue.

    SELECT ISNULL(m.MovieID,0) AS MovieID, 
           ISNULL(g.GenreID,0) AS GenreID, 
           ISNULL(mg.MovieGenreID,0) AS MovieGenreID,
           ISNULL(g.Genre,'') AS Genre
    FROM --rest of your query...
    
    0 讨论(0)
  • 2021-02-06 20:52

    The simplest answer is to replace the nulls with non-null values. Try:

    ALTER PROCEDURE usp_GetMovieGenreByMovieID
    @MovieID int
    AS
    BEGIN
        BEGIN TRY
            SELECT m.MovieID, 
                   coalesce(g.GenreID,0) GenreID, 
                   coalesce(mg.MovieGenreID,0) MovieGenreID, 
                   coalesce(g.Genre, 'Not Applicable') Genre
            FROM Movie AS m
            LEFT JOIN MovieGenre AS mg
                ON m.MovieId = mg.MovieID
            LEFT JOIN Genre AS g
                ON mg.GenreID = g.GenreID
            WHERE m.MovieID = @MovieID
        END TRY
        BEGIN CATCH
            RAISERROR ('Error while trying to receive genre(s).',16,1)
        END CATCH
    END
    
    0 讨论(0)
  • 2021-02-06 20:54

    In my case I was using EF Core and the issue was that the field was nullable in the database but in the ModelCreating it was required like that:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
       modelBuilder.Entity<MyEntity>(entity =>
       {
          entity.Property(e => e.Details)
                        .IsRequired()
                        .HasMaxLength(250);
       }
    }
    

    I remove the IsRequired() and it worked fine.

    Update few days after, Got same issue, a string field It was not allowing null in the DB.

    0 讨论(0)
  • 2021-02-06 20:57

    BookingQuantity - column having null value in DB. but actual DB BookingQuantity not null column. Some rare case it happens to enter. In that case below code throw error the same error(Data is Null. This method or property cannot be called on Null values ).

    totalBookingQuantity += item.InspPoTransactions.Where(x => x.PoId == inspectionPODetail.PoId && x.ProductId == inspectionPODetail.ProductId).Sum(x => Convert.ToInt32(x.BookingQuantity));

    0 讨论(0)
  • 2021-02-06 20:57

    Today I've faced this issue. But mine has been fixed in another way. If someday anyone stumbled the answer is for them.

    As this is a generic C# .NET CLI exception

    I've added a new foreign key to one of my DB table with no default value. Thus the value was set to NULLas that column was set to allow null. And I've been getting this exception while querying on that table.

    As solution, I replaced the NULL values with appropriate values (as they are foreign key' they should be appropriate).

    That's all.

    Thank you.

    0 讨论(0)
提交回复
热议问题