C# 无法在实体框架核心5中设置所有者拥有类型和属性的索引
我正在尝试使用该实体的自有类型和普通属性在实体上创建索引。 这是我的密码:C# 无法在实体框架核心5中设置所有者拥有类型和属性的索引,c#,.net,.net-core,entity-framework-core,C#,.net,.net Core,Entity Framework Core,我正在尝试使用该实体的自有类型和普通属性在实体上创建索引。 这是我的密码: public class ApplicationUser : IdentityUser<long> { public Name Name { get; private set; } protected ApplicationUser() { } private ApplicationUser(ApplicationUserName name) {
public class ApplicationUser : IdentityUser<long>
{
public Name Name { get; private set; }
protected ApplicationUser()
{
}
private ApplicationUser(ApplicationUserName name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
}
}
public class Name : ValueObject
{
public string FirstName { get; }
public string LastName { get; }
protected Name()
{
}
private Name(string firstName, string lastName)
: this()
{
FirstName = firstName;
LastName = lastName;
}
public static Result<Name> Create(string firstName, string lastName)
{
if (string.IsNullOrWhiteSpace(firstName))
return Result.Failure<Name>("First name should not be empty");
if (string.IsNullOrWhiteSpace(lastName))
return Result.Failure<Name>("Last name should not be empty");
return Result.Success(new Name(firstName, lastName));
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return FirstName;
yield return LastName;
}
}
modelBuilder.Entity<ApplicationUser>(entity =>
{
entity.OwnsOne(p => p.Name, p =>
{
p.Property(pp => pp.FirstName)
.IsRequired()
.HasColumnName("FirstName")
.HasMaxLength(64);
p.Property(pp => pp.LastName)
.IsRequired()
.HasColumnName("LastName")
.HasMaxLength(64);
});
entity.HasIndex(p => new { p.Name.FirstName, p.Name.LastName, p.Email });
});
公共类应用程序用户:IdentityUser
{
公共名称名称{get;private set;}
受保护的ApplicationUser()
{
}
专用ApplicationUser(ApplicationUserName名称)
{
Name=Name??抛出新ArgumentNullException(nameof(Name));
}
}
公共类名称:ValueObject
{
公共字符串名{get;}
公共字符串LastName{get;}
受保护名称()
{
}
私有名称(字符串名、字符串名)
:此()
{
名字=名字;
LastName=LastName;
}
公共静态结果创建(stringfirstname、stringlastname)
{
if(string.IsNullOrWhiteSpace(firstName))
返回结果。失败(“名字不应为空”);
if(string.IsNullOrWhiteSpace(lastName))
返回结果。失败(“姓氏不应为空”);
返回Result.Success(新名称(firstName,lastName));
}
受保护的覆盖IEnumerable GetEqualityComponents()
{
返回名字;
返回姓氏;
}
}
modelBuilder.Entity(Entity=>
{
entity.OwnsOne(p=>p.Name,p=>
{
p、 属性(pp=>pp.FirstName)
.IsRequired()
.HasColumnName(“名字”)
.HasMaxLength(64);
p、 属性(pp=>pp.LastName)
.IsRequired()
.HasColumnName(“姓氏”)
.HasMaxLength(64);
});
entity.HasIndex(p=>new{p.Name.FirstName,p.Name.LastName,p.Email});
});
当我运行添加迁移时,会出现以下错误:
The expression 'p => new <>f__AnonymousType3`3(FirstName = p.Name.FirstName, LastName = p.Name.LastName, Email = p.Email)' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. When specifying multiple properties or fields, use an anonymous type: 't => new { t.MyProperty, t.MyField }'. (Parameter 'memberAccessExpression')
表达式“p=>new f_uanonymoustype3`3(FirstName=p.Name.FirstName,LastName=p.Name.LastName,Email=p.Email)”不是有效的成员访问表达式。表达式应该表示一个简单的属性或字段访问:“t=>t.MyProperty”。指定多个属性或字段时,请使用匿名类型:“t=>new{t.MyProperty,t.MyField}”。(参数“memberAccessExpression”)
有人知道我的场景是否可以使用实体框架创建索引吗?我的谷歌搜索更好。检查此问题: