problem in creation of nullable property in CodeFirst approach

Viewed 22

.I created project with .net core 6. this is group model: enter image description here

GroupImageName is nullable but when I do migration, this property is created non-nullabe,also for example, posts property is created non-nullable.group model is made as follows:

enter image description here

the context class:

enter image description here

1 Answers

To make a nullable properties from code first you have to use Nullable<string> or string? which makes your property nullable in database after migration

E.g. in your case your model looks like below:

PostGroup.CS

Public class PostGroup
{
   public int GroupId{get;set;}
   public string GroupName {get;set;}
   public string? GroupImageName {get;set;}
   .
   .
   .
   n so on properties
}

after making above change just run the command like below from PMC

add-migration
update-database

above stapes will make your properties nullable in db after successful migration

Related