What should you do?

You need to ensure that an exception is thrown when color names are set to less than two characters. What should you do?

You need to ensure that an exception is thrown when color names are set to less than two characters. What should you do?

A.
Add the following method to the Color partial class in Model\Color.cs:
protected overrride void OnPropertyChanged(string property)
{
if (property == “Name” && this.Name.Length < 2)
throw new ArgumentOutOfRangeException(“Name must be at least two characters”);
}

B.
Add the following code segment to the ContosoEntities partial class in Model\ContosoEntities.cs:
public override in SaveChanges(System.Data.Objects.SaveOptions options)
{
var changes = this.ObjectStateManager.GetObjectSateEntries(System.Data.EntityState.Added);
foreach (var change in changes)
{
if (change.Entity is Color)
if (((Color)change.Entity.Name.Length < 2) throw new ArgumentException(“Name too short”);
}
return base.SaveChanges(options);
}

C.
Add the following attribute to the Name property of the Color class in the entity designer file:
[StringLength(256, MinimumLength = 2)]

D.
Add the following method to the Color partial class in Model\Color.cs:
protected overrride void OnPropertyChanging(string property)
{
if (property == “Name” && this.Name.Length < 2)
throw new ArgumentOutOfRangeException(“Name must be at least two characters”);
}



Leave a Reply 5

Your email address will not be published. Required fields are marked *


E

E

Isn’t D a better answer?

J

J

I do not think so – because this.Name in answer D would return the previous value

John Galt

John Galt

Good call.