Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.Net核心域模型中用值对象替换基本类型的验证?_C#_Asp.net Core_Domain Driven Design - Fatal编程技术网

C# 如何在.Net核心域模型中用值对象替换基本类型的验证?

C# 如何在.Net核心域模型中用值对象替换基本类型的验证?,c#,asp.net-core,domain-driven-design,C#,Asp.net Core,Domain Driven Design,.Net核心3 通过示例,我尝试使用值对象创建域模型的验证。 但我有一个错误 // Value object TitleValue public class TitleValue : ValueObject { public string Value { get; } public TitleValue() { } public TitleValue(string value) {

.Net核心3

通过示例,我尝试使用值对象创建域模型的验证。 但我有一个错误

// Value object TitleValue
 public class TitleValue : ValueObject
    {

        public string Value { get; }

        public TitleValue()
        {
        }

        public TitleValue(string value)
        {
            Value = value;
            Create(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;
        }
    }
//值对象标题值
公共类标题值:ValueObject
{
公共字符串值{get;}
公共标题值()
{
}
公共标题值(字符串值)
{
价值=价值;
创造(价值);
}
公共静态结果创建(字符串输入)
{
if(string.IsNullOrWhiteSpace(输入))
返回Result.Fail(Errors.General.ValueIsRequired().Serialize());
字符串title=input.Trim();
如果(标题长度>2)
返回Result.Fail(Errors.General.valueIsToLong().Serialize());
返回结果.Ok(新标题值(标题));
}
受保护的覆盖IEnumerable GetEqualityComponents()
{
收益回报值;
}
}
//域模型
公共类导航
{
[关键]
公共int Id{get;set;}
公共标题值标题{get;set;}//出现问题
公共字符串Slug{get;set;}
公共字节分类{get;set;}
公共整数排序{get;set;}
公共int父项{get;set;}
公共字符串路径{get;set;}
公共ICollection角色授权{get;set;}
}
我得到一个错误:

处理请求时发生未处理的异常。 InvalidOperationException:实体类型“TitleValue”需要定义主键。如果要使用无键实体类型,请调用“HasNoKey()”。 Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonullPrimaryKeys(IModel模型,IDiagnosticsLogger)

Net核心认为,标题值是一个实体


如何修复此错误并获得良好的验证?

您必须告诉EF TitleValue是一个值对象

在模型创建上的

    builder.Entity<Navigation>().OwnsOne(n => n.TitleValue, n => n.Property(p => p.Value).HasColumnName("Title"));
其中
OwnsOne
TitleValue
设置为值对象(问题已解决)


第二个表达式告诉EF您希望数据库列名为“Title”,而不是默认的“Title\u值”。

您必须告诉EF TitleValue是一个值对象

在模型创建上的

    builder.Entity<Navigation>().OwnsOne(n => n.TitleValue, n => n.Property(p => p.Value).HasColumnName("Title"));
其中
OwnsOne
TitleValue
设置为值对象(问题已解决)

第二个表达式告诉EF您希望数据库列名为“Title”,而不是默认的“Title\u值”

    builder.OwnsOne(n => n.TitleValue, n => n.Property(p => p.Value).HasColumnName("Title"));