Can I have constructor in my EF model?
Can I have constructor to create the model based off of the DTO?
Right now I have a constructor to make a DTO based off of the model
4 Replies
my model:
and my DTO:
c#
public class DynamicContent
{
[Key]
public int Id { get; set; }
[Column(TypeName = "varchar(255)")]
[Required]
public string Name { get; set; }
public string? TextEnglish { get; set; }
public string? TextBosnian { get; set; }
[Column(TypeName = "varchar(100)")]
public string? ImageCaptionEnglish { get; set; }
[Column(TypeName = "varchar(100)")]
public string? ImageCaptionBosnian { get; set; }
[Column(TypeName = "varchar(254)")]
public string? ImageUrl { get; set; }
[Column(TypeName = "varbinary(max)")]
public byte[]? ImageData { get; set; }
[Column(TypeName = "varchar(100)")]
public string? IconValue { get; set; }
public int? OrderIndex { get; set; }
[Column(TypeName = "varchar(100)")]
[Required]
public string ModifiedBy { get; set; }
[Required]
public DateTime ModifiedOn { get; set; }
}
c#
public class DynamicContent
{
[Key]
public int Id { get; set; }
[Column(TypeName = "varchar(255)")]
[Required]
public string Name { get; set; }
public string? TextEnglish { get; set; }
public string? TextBosnian { get; set; }
[Column(TypeName = "varchar(100)")]
public string? ImageCaptionEnglish { get; set; }
[Column(TypeName = "varchar(100)")]
public string? ImageCaptionBosnian { get; set; }
[Column(TypeName = "varchar(254)")]
public string? ImageUrl { get; set; }
[Column(TypeName = "varbinary(max)")]
public byte[]? ImageData { get; set; }
[Column(TypeName = "varchar(100)")]
public string? IconValue { get; set; }
public int? OrderIndex { get; set; }
[Column(TypeName = "varchar(100)")]
[Required]
public string ModifiedBy { get; set; }
[Required]
public DateTime ModifiedOn { get; set; }
}
c#
[JsonConstructor]
public DynamicContentDTO() { }
public DynamicContentDTO(DynamicContent d)
{
Id = d.Id;
Name = d.Name;
TextEnglish = d.TextEnglish;
TextBosnian = d.TextBosnian;
ImageCaptionEnglish = d.ImageCaptionEnglish;
ImageCaptionBosnian = d.ImageCaptionBosnian;
ImageUrl = d.ImageUrl;
ImageData = d.ImageData;
IconValue = d.IconValue;
OrderIndex = d.OrderIndex;
}
[Required(ErrorMessage = "Id is required.")]
public int? Id { get; set; }
[Required(ErrorMessage = "Name is required.")]
public string? Name { get; set; }
public string? TextEnglish { get; set; }
public string? TextBosnian { get; set; }
public string? ImageCaptionEnglish { get; set; }
public string? ImageCaptionBosnian { get; set; }
public string? ImageUrl { get; set; }
public byte[]? ImageData { get; set; }
public string? IconValue { get; set; }
public int? OrderIndex { get; set; }
}
c#
[JsonConstructor]
public DynamicContentDTO() { }
public DynamicContentDTO(DynamicContent d)
{
Id = d.Id;
Name = d.Name;
TextEnglish = d.TextEnglish;
TextBosnian = d.TextBosnian;
ImageCaptionEnglish = d.ImageCaptionEnglish;
ImageCaptionBosnian = d.ImageCaptionBosnian;
ImageUrl = d.ImageUrl;
ImageData = d.ImageData;
IconValue = d.IconValue;
OrderIndex = d.OrderIndex;
}
[Required(ErrorMessage = "Id is required.")]
public int? Id { get; set; }
[Required(ErrorMessage = "Name is required.")]
public string? Name { get; set; }
public string? TextEnglish { get; set; }
public string? TextBosnian { get; set; }
public string? ImageCaptionEnglish { get; set; }
public string? ImageCaptionBosnian { get; set; }
public string? ImageUrl { get; set; }
public byte[]? ImageData { get; set; }
public string? IconValue { get; set; }
public int? OrderIndex { get; set; }
}
I would say no, your ef entity should not know about your dto
EF entities should be, IMHO, as dumb as possible. Just a class with a bunch of props and that's all
ok. thank you !