I have an entity with a value object as one of its properties. When I run db- migrations entity framework core7 returns the error:
No suitable constructor was found for entity type 'Candidate'. The following constructors had parameters that could not be bound to properties of the entity type: Cannot bind 'spouse' in 'Candidate(string yearAddmitted, string programmeType, string programmeCode, string programme, string department, string studyMode, string entryMode, byte duration, string reasonForTransfer, bool isHandicapped, DateTime? admissionDate, byte admissionStage, string whyUs, string admissionStatus, string surname, string firstname, string middleName, string sex, string residentialAddress, string permanentHomeAdd, Spouse spouse, string state, string lg, string country, string phone, string email, string maritalStatus, int? bDay, string bMonth, int? bYear, NextOfKin nextOfKin, string photoPath, string status)' bellow is the Entity:
public class Candidate
{
public string CandidateId { get; }
//Academic Properties
public string YearAddmitted { get; private set; }
public string ProgrammeType { get; private set; }
public string ProgrammeCode { get; private set; }
public string Programme { get; private set; }
public string Department { get; private set; }
public string StudyMode { get; private set; }
public string EntryMode { get; private set; }
public byte Duration { get; private set; }
public string ReasonForTransfer { get; private set; }
public bool IsHandicapped { get; private set; }
public DateTime? AdmissionDate { get; private set; }
public byte AdmissionStage { get; private set; }
public string WhyUs { get; private set; } = string.Empty;
public string AdmissionStatus { get; private set; } = string.Empty;
public JambResult? JambResult { get; }
public List<OLevelResult>? OlevelResults { get; }
public List<OtherAcademicQualification>? OtherQualifications { get; }
//Personal Data
public string Surname { get; private set; } = string.Empty;
public string Firstname { get; private set; } = string.Empty;
public string MiddleName { get; private set; } = string.Empty;
public string Sex { get; private set; } = string.Empty;
public string ResidentialAddress { get; private set; } = string.Empty;
public string PermanentHomeAdd { get; private set; } = string.Empty;
public string State { get; private set; } = string.Empty;
public string Lg { get; private set; } = string.Empty;
public string Country { get; private set; } = string.Empty;
public string Phone { get; private set; } = string.Empty;
public string MaritalStatus { get; private set; } = string.Empty;
public string Email { get; private set; } = string.Empty;
public string MailingAddress { get; private set; } = string.Empty;
public int? BDay { get; private set; }
public string BMonth { get; private set; } = string.Empty;
public int? BYear { get; private set; }
public string PhotoPath { get; private set; } = string.Empty;
public string Status { get; private set; } = string.Empty;
public Spouse Spouse { get;}
private Candidate(string yearAddmitted, string programmeType, string programmeCode, string programme, string department,
string studyMode, string entryMode, byte duration, string reasonForTransfer, bool isHandicapped,
DateTime? admissionDate, byte admissionStage, string whyUs, string admissionStatus, string surname,
string firstname, string middleName, string sex, string residentialAddress, string permanentHomeAdd,Spouse spouse, string state, string lg, string country,
string phone, string email, string maritalStatus, int? bDay, string bMonth, int? bYear,string photoPath,string status)
{
CandidateId = GenerateCandidateId(yearAddmitted);
YearAddmitted = yearAddmitted;
ProgrammeType = programmeType;
ProgrammeCode = programmeCode;
Programme = programme;
Department = department;
StudyMode = studyMode;
EntryMode = entryMode;
Duration = duration;
ReasonForTransfer = reasonForTransfer;
IsHandicapped = isHandicapped;
AdmissionDate = admissionDate;
AdmissionStage = admissionStage;
WhyUs = whyUs;
AdmissionStatus = admissionStatus;
Surname = surname; Firstname = firstname; MiddleName = middleName; Sex = sex;
ResidentialAddress = residentialAddress;
PermanentHomeAdd = permanentHomeAdd; Spouse = spouse;
State = state; Lg = lg; Country = country; Phone = phone; Email = email; MaritalStatus = maritalStatus;
BDay = bDay;BMonth = bMonth; BYear = bYear; PhotoPath = photoPath;
Status = status;
}
public string Name
{
get
{
if (!string.IsNullOrEmpty(MiddleName))
{
return Surname.Trim() + ", " + Firstname.Trim() + " " + MiddleName.Trim();
}
else
{
return Surname.Trim() + ", " + Firstname.Trim();
}
}
}
public string? BirthDate
{
get
{
if (this.BDay > 0 && this.BYear > 0)
{
return BDay.ToString() + "-" + BMonth + "-" + BYear.ToString();
}
else
return null;
}
}
public static Candidate Create(string yearAddmitted, string programmeType, string programmeCode, string programme, string department,string studyMode,
string entryMode, byte duration, string reasonForTransfer, bool isHandicapped, DateTime? admissionDate,
byte admissionStage, string whyUs, string admissionStatus, string surname, string firstname, string middleName,
string sex, string residentialAddress, string permanentHomeAdd, Spouse spouse,string state, string lg,
string country, string phone, string email, string maritalStatus, int? bDay, string bMonth, int? bYear,
string photoPath, string status)
{
return new Candidate(yearAddmitted, programmeType, programmeCode, programme, department, studyMode, entryMode,
duration, reasonForTransfer, isHandicapped, admissionDate, admissionStage, whyUs, admissionStatus, surname,
firstname, middleName, sex, residentialAddress, permanentHomeAdd, spouse,
state, lg, country, phone, email, maritalStatus, bDay, bMonth, bYear, photoPath, idMode, status);
}
}
my value objects
public class Spouse
{
public string FullName { get; private set; }
public string Address { get; private set; }
private Spouse(string fullName, string address)
{
FullName = fullName;
Address = address;
}
public static Spouse Create(string fullName, string address)
{
return new Spouse(fullName, address);
}
}
Candidate configuration
public class CandidateConfiguration:IEntityTypeConfiguration<Candidate>
{
public void Configure(EntityTypeBuilder<Candidate> builder)
{
builder.Property(c => c.CandidateId).HasMaxLength(20);
builder.HasKey(c => c.CandidateId);
builder.Property(c => c.AdmissionStatus).HasMaxLength(20);
builder.Property(c => c.BMonth).HasMaxLength(10);
builder.Property(c => c.Country).HasMaxLength(70);
builder.Property(c => c.State).HasMaxLength(100);
builder.Property(c => c.Status).HasMaxLength(15).IsRequired();
builder.Property(c => c.StudyMode).HasMaxLength(15).IsRequired();
builder.Property(c => c.Surname).HasMaxLength(20).IsRequired();
builder.Property(c => c.WhyUs).HasMaxLength(200);
builder.Property(c => c.YearAddmitted).HasMaxLength(10).IsRequired();
builder.Property(c => c.Firstname).HasMaxLength(20).IsRequired();
builder.Property(c => c.MiddleName).HasMaxLength(20);
builder.Property(c => c.Sex).HasMaxLength(10);
builder.Property(c => c.Lg).HasMaxLength(30);
builder.Property(c => c.MailingAddress).HasMaxLength(100);
builder.Property(c => c.MaritalStatus).HasMaxLength(15);
builder.Property(c => c.PermanentHomeAdd).HasMaxLength(100);
builder.Property(c => c.Phone).HasMaxLength(15).IsRequired();
builder.Property(c => c.PhotoPath).HasMaxLength(500);
builder.Property(c => c.Email).HasMaxLength(100).IsRequired();
builder.Property(c => c.IsHandicapped).HasMaxLength(3);
builder.Property(c => c.MaritalStatus).HasMaxLength(20);
builder.Property(c => c.ProgrammeCode).HasMaxLength(50);
builder.Property(c => c.ProgrammeType).HasMaxLength(20);
builder.Property(c => c.Programme).HasMaxLength(70);
builder.Property(c => c.ReasonForTransfer).HasMaxLength(100);
builder.Property(c => c.ResidentialAddress).HasMaxLength(100);
builder.OwnsOne(c => c.Spouse, sp =>
{
sp.WithOwner();
sp.Property(nk => nk.Address).HasMaxLength(100);
sp.Property(nk => nk.FullName).HasMaxLength(100);
});
builder.Navigation(x => x.Spouse).IsRequired(false);
builder.HasOne(c => c.JambResult).WithOne();
builder.HasMany(c => c.OtherQualifications).WithOne();
builder.HasMany(a => a.OlevelResults).WithOne();
}
}
Why is EFCore having complaining about the value object 'Spouse'?