EF Core 6 - Why it is NOT Nullable?

Cenk 1,001 Reputation points
2022-09-18T18:44:29.817+00:00

Hi,

Here is my entity;

public class OrderDetail  
    {  
        public int Id { get; set; }  
          
        [Required]  
        [MaxLength(100)]  
        public string ProductCode { get; set; }  
        [Required]  
        [MaxLength(250)]  
        public string ProductName { get; set; }  
        [Required]  
        public int Quantity { get; set; }  
        [Required]  
        public double BuyUnitPrice { get; set; }  
        public double CostRatio { get; set; }  
        public double UnitCost { get; set; }  
        public double TotalBuyPrice { get; set; }  
        public double SellUnitPrice { get; set; }  
        public double TotalSellPrice { get; set; }  
        [MaxLength(150)]  
        public string ShippingNumber { get; set; }  
        public string Status { get; set; }  
        [MaxLength(150)]  
        public string TrackingNumber { get; set; }  
        [MaxLength(400)]  
        public string Description { get; set; }  
        public string Currency { get; set; }  
        public string CustomerStockCode { get; set; }  
        public string CustomerOrderNumber { get; set; }  
        public int IsActive { get; set; }  
        public double TotalUnitCost { get; set; }  
        public int OrderId { get; set; }  
        public int VendorId { get; set; }  
        public Order Order { get; set; }  
        public Vendor Vendor { get; set; }  
         
    }  

Here is migration:

modelBuilder.Entity("IMS.CoreBusiness.OrderDetail", b =>  
                {  
                    b.Property<int>("Id")  
                        .ValueGeneratedOnAdd()  
                        .HasColumnType("int");  
  
                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);  
  
                    b.Property<double>("BuyUnitPrice")  
                        .HasColumnType("float");  
  
                    b.Property<double>("CostRatio")  
                        .HasColumnType("float");  
  
                    b.Property<string>("Currency")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("CustomerOrderNumber")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("CustomerStockCode")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("Description")  
                        .IsRequired()  
                        .HasMaxLength(400)  
                        .HasColumnType("nvarchar(400)");  
  
                    b.Property<int>("IsActive")  
                        .HasColumnType("int");  
  
                    b.Property<int>("OrderId")  
                        .HasColumnType("int");  
  
                    b.Property<string>("ProductCode")  
                        .IsRequired()  
                        .HasMaxLength(100)  
                        .HasColumnType("nvarchar(100)");  
  
                    b.Property<string>("ProductName")  
                        .IsRequired()  
                        .HasMaxLength(250)  
                        .HasColumnType("nvarchar(250)");  
  
                    b.Property<int>("Quantity")  
                        .HasColumnType("int");  
  
                    b.Property<double>("SellUnitPrice")  
                        .HasColumnType("float");  
  
                    b.Property<string>("ShippingNumber")  
                        .IsRequired()  
                        .HasMaxLength(150)  
                        .HasColumnType("nvarchar(150)");  
  
                    b.Property<string>("Status")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<double>("TotalBuyPrice")  
                        .HasColumnType("float");  
  
                    b.Property<double>("TotalSellPrice")  
                        .HasColumnType("float");  
  
                    b.Property<double>("TotalUnitCost")  
                        .HasColumnType("float");  
  
                    b.Property<string>("TrackingNumber")  
                        .IsRequired()  
                        .HasMaxLength(150)  
                        .HasColumnType("nvarchar(150)");  
  
                    b.Property<double>("UnitCost")  
                        .HasColumnType("float");  
  
                    b.Property<int>("VendorId")  
                        .HasColumnType("int");  
  
                    b.HasKey("Id");  
  
                    b.HasIndex("OrderId");  
  
                    b.HasIndex("VendorId");  
  
                    b.ToTable("OrdersDetail");  
                });  

Why are some of the properties IsRequired even though they are not marked Required in the entity?

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
742 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Laxmikant 221 Reputation points
    2022-10-02T12:39:35.18+00:00

    your databse does not allow null values for the column AssistentResponsibleName remove the question mark from property definition like

     public string AssistantResponsibleName { get; set; }  
    
    0 comments No comments

  2. clark wu 0 Reputation points
    2023-10-20T06:53:26.03+00:00

    Just need to remove this line "enable" on .csproj file

    <Nullable>enable</Nullable>

    https://stackoverflow.com/a/57756254

    https://stackoverflow.com/a/72725423

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.