My class inherits an interface so I need to have the emailaddress property in the Person class.
My question is what is the best way to get the property and set the property
public class Contact : IPerson, ILocation
{
...
[MaxLength(256)]
public string EmailAddress {
get{
return this.Emails.First().ToString();
}
set{ ???? }
}
....
public virtual ICollection<Emails> Emails { get; set; }
}
Essentially, I’m trying to get the class to allow more than one email.
For full disclosure, I’m pretty new to this and I may not be asking the correct questions, but I’ve searched for a day and half and haven’t seen anything like this (not that I think it’s unusual) and could use the insight.
Email class properties:
[Key]
public int Id { get; set; }
[MaxLength(256)]
public string EmailAddress { get; set; }
Do you have control over the design of the
IPersoninterface that is forcing you to implementEmailAddress? If so, I would recommend rethinking the design to avoid needing both a singular property and a list of email addresses on the same object.You may also want to consider making the
Emailsproperty setterprotected, to prevent outside code from changing your object’s data.If you must implement to this interface, and you wish to have the
EmailAddressproperty always refer to the first email in the collection, you can try this code.