© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
54 replies
Maverick

Default parameter values in primary constructor

I've created a data model class for use with Entity Framework Core, and I just had a question regarding my use of a primary constructor here. As you can see,
IsActive
IsActive
and
Prizes
Prizes
are both being initialized to default values. Will this give me the same behaviour as if I'd used an old-school constructor? (i.e.
public RaffleModel(...)
public RaffleModel(...)
... you know the drill)

Additionally, are there any considerations I need to have specifically relating to how Entity Framework Core will interact with this class?

public class RaffleModel
    (string title, string description, DateTime drawDate, bool isBundle, string raffleHolderId)
{
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int RaffleId { get; set; } // auto-generated
    
    [Required] 
    [StringLength(30)] 
    public string Title { get; set; } = title;
    
    [Required]
    [StringLength(500)]
    public string Description { get; set; } = description;
    
    [Required]
    public DateTime DrawDate { get; set; } = drawDate;

    // Set to true when the raffle is published (after prizes are added)
    [Required] 
    public bool IsActive { get; set; } = false; 
    
    [Required]
    public bool IsBundle { get; set; } = isBundle;
    
    [ForeignKey("AspNetUsers")]
    public string RaffleHolderId { get; set; } = raffleHolderId;
    
    // Identity user reference
    public virtual ApplicationUser? RaffleHolder { get; set; }
    
    // Navigation property (initialised to prevent null references)
    public virtual ICollection<PrizeModel> Prizes { get; set; } = new HashSet<PrizeModel>();
}
public class RaffleModel
    (string title, string description, DateTime drawDate, bool isBundle, string raffleHolderId)
{
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int RaffleId { get; set; } // auto-generated
    
    [Required] 
    [StringLength(30)] 
    public string Title { get; set; } = title;
    
    [Required]
    [StringLength(500)]
    public string Description { get; set; } = description;
    
    [Required]
    public DateTime DrawDate { get; set; } = drawDate;

    // Set to true when the raffle is published (after prizes are added)
    [Required] 
    public bool IsActive { get; set; } = false; 
    
    [Required]
    public bool IsBundle { get; set; } = isBundle;
    
    [ForeignKey("AspNetUsers")]
    public string RaffleHolderId { get; set; } = raffleHolderId;
    
    // Identity user reference
    public virtual ApplicationUser? RaffleHolder { get; set; }
    
    // Navigation property (initialised to prevent null references)
    public virtual ICollection<PrizeModel> Prizes { get; set; } = new HashSet<PrizeModel>();
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Primary Constructor
C#CC# / help
5mo ago
Primary constructor parameter is null after being run in a Task
C#CC# / help
3y ago
`required` vs primary constructor
C#CC# / help
8mo ago
primary constructor naming convention
C#CC# / help
2y ago