【发布时间】:2022-08-08 23:04:18
【问题描述】:
我正在尝试将我的一个模块从 Postgres(使用 EF)迁移到 Cassandra。 这是我对 Cassandra 映射的最佳尝试:
internal sealed class UserMappings : Mappings
{
public UserMappings()
{
For<User>().TableName(\"users\")
.PartitionKey(x => x.Id)
.ClusteringKey(x => x.Id)
.Column(x => x.Id, x => x.WithDbType<Guid>().WithName(\"id\"))
// I want to add mappings for password Hash here
}
}
第一个问题是我使用 VO 来确保安全,但想将原语存储在数据库中。 实体 id 的示例 VO:
public record UserId
{
public Guid Value { get; }
public UserId(Guid value)
{
if (value == Guid.Empty) throw new Exception(\"Invalid UserId\");
Value = value;
}
public static implicit operator Guid(UserId id) => id.Value;
public static implicit operator UserId(Guid id) => new(id);
}
其次,我的实体有私有字段,我不知道如何将它们映射到数据库。
internal class User
{
private User()
{
}
public User(/*...*/)
{
//...
}
private string _passwordHash;
public UserId Id { get; }
//...
}
还需要公共无参数构造函数吗?
-
关于如何提出好问题的友好说明。一般指导是您 (a) 提供问题的良好摘要,包括软件/组件版本、完整的错误消息 + 完整的堆栈跟踪; (b) 描述您尝试解决问题的方法、您所做调查的详细信息; (c) 复制问题的最小示例代码。干杯!
-
我无法在本地使用您的代码,因此无法重现该问题。没有定义类型 \"Email, \"FirstName\", \"RefreshToken\", \"PasswordHash\" \"InvalidUserIdException\"。如果这些类型不是重现问题所必需的,则不要添加这些字段或只使用原生类型,如字符串和异常。
-
另外,“我的大部分领域都有 VO”是什么意思,VO 是什么意思?
-
@JoãoReis VO 对我来说是类/记录,其中包含一个属性以避免原始痴迷,并且使用它们更安全,例如,当参数顺序更改时,它不会编译。我将删除我的代码中不必要的部分:)
标签: c# .net cassandra datastax-csharp-driver