How to in .Net core domain model replace validation of primitive types with value objects?

.Net Core 3

By this and this examples I try create validation of domain model with value objects. But I get error.

// Value object TitleValue
 public class TitleValue : ValueObject

        public string Value { get; }

        public TitleValue()

        public TitleValue(string value)
            Value = value;

        public static Result<TitleValue> Create(string input)
            if (string.IsNullOrWhiteSpace(input))
                return Result.Fail<TitleValue>(Errors.General.ValueIsRequired().Serialize());

            string title = input.Trim();

            if (title.Length > 2)
                return Result.Fail<TitleValue>(Errors.General.ValueIsTooLong().Serialize());

            return Result.Ok(new TitleValue(title));

        protected override IEnumerable<object> GetEqualityComponents()
            yield return Value;
// Domain model 
 public class Navigation
        public int Id { get; set; }

        public TitleValue Title { get; set; } // there is problem
        public string Slug { get; set; }
        public byte IsCategory { get; set; }
        public int Sort { get; set; }
        public int Parent { get; set; }
        public string Path { get; set; }
        public ICollection<EdRoleNavigation> RoleNavigations { get; set; }

I get error:

An unhandled exception occurred while processing the request. InvalidOperationException: The entity type ‘TitleValue’ requires a primary key to be defined. If you intended to use a keyless entity type call ‘HasNoKey()’. Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model, IDiagnosticsLogger logger)

.Net core thinks, that the TitleValue is an entity.

How I can fix this error, and get a nice validation?


The NotMapped attribute is used to specify that an entity or property is not to be mapped to a table or column in the database.

Leave a Reply

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