MongoDB复合键:InvalidOperationException:{document} .Identity不受支持

时间:2017-08-08 23:34:14

标签: c# mongodb composite-primary-key

我遇到了为一个由复合ID组成的类的问题,而该复合ID又有一个基类,我收到错误说InvalidOperationException: {document}.Identity is not supported.

我试图写入数据库的类如下:

public class Product : IEntity<Product>
{
    public readonly Sku Sku;
    public string Name { get; private set; }
    public string Description { get; private set; }
    public bool IsArchived { get; private set; }
    public Identity<Product> Identity => Sku;

    public Product(Sku sku, string name, bool isArchived)
    {
        Sku = sku;
        Name = name;
        IsArchived = isArchived;
    }
}

public interface IEntity<T>
{
    Identity<T> Identity { get; }
}

反过来有一个ID Sku,它是由以下复合值(VendorIdValue中的本地Sku)组成的类:

public class Sku : Identity<Product>
{
    public readonly VendorId VendorId;
    public readonly string Value;

    public Sku(VendorId vendorId, string value)
    {
        VendorId = vendorId;
        Value = value;
    }

    protected override IEnumerable<object> GetIdentityComponents()
    {
        return new object[] {VendorId, Value};
    }
}

public class VendorId : Identity<Vendor>
{
    public readonly string Value;

    public VendorId(string value)
    {
        Value = value;
    }

    protected override IEnumerable<object> GetIdentityComponents()
    {
        return new object[] {Value};
    }
}

我的实体Identity有一个基类,我在我的DDD库中使用,基本上这里的ToString()输出可以用作ID,如果这样可以简化:

public abstract class Identity<T> : IEquatable<Identity<T>>
{
    public override bool Equals(object obj) { /* snip */ }
    public bool Equals(Identity<T> other) { /* snip */ }
    public override int GetHashCode() { /* snip */ }

    public override string ToString()
    {
        var id = string.Empty;

        foreach (var component in GetIdentityComponents())
        {
            if (string.IsNullOrEmpty(id))
                id = component.ToString(); // first item, dont add a divider
            else
                id += "." + component;
        }

        return id;
    }

    protected abstract IEnumerable<object> GetIdentityComponents();
}

我在app start上注册了映射:

// rehydrate readonly properties via matched constructor
// https://stackoverflow.com/questions/39604820/serialize-get-only-properties-on-mongodb
ConventionRegistry
    .Register(nameof(ImmutablePocoConvention), new ConventionPack { new ImmutablePocoConvention() }, _ => true);

BsonClassMap.RegisterClassMap<Product>(cm =>
{
    cm.AutoMap();
    cm.MapIdMember(c => c.Sku);
});

BsonClassMap.RegisterClassMap<Vendor>(cm =>
{
    cm.AutoMap();
    cm.MapIdMember(c => c.Id);
});

然而,当我去写作时,我得到InvalidOperationException: {document}.Identity is not supported.

// my respositoru method
public void Upsert<T>(T entity) where T : IEntity<T>
{
    this.Database
        .GetCollection<T>(product.GetType().FullName)()
        .ReplaceOneAsync(x=>x.Identity.Equals(entity.Identity), entity, new UpdateOptions() {IsUpsert = true})
        .Wait();
}

var product = new Product(new Sku(new VendorId("dell"), "12434" ),"RAM", false );
myProductRepo.Upsert(product);

由于我直接从我的实体层持续存在(或者如果我只使用自动播放器和更简单的POCO),或者如果我缺少一些映射指令,我不确定这是否过于复杂。

感谢任何帮助或指示。

0 个答案:

没有答案