Litedb: Sample Update for Cross References

Created on 1 Dec 2017  路  2Comments  路  Source: mbdavid/LiteDB

Hello, I am very thankful and impressed with this lib.

Can you please update this sample DbRef for cross references to show CRUD updates, deletes, Upsert.

I am getting errors when I try to do cross ref. in V4 is this natively supported with multiple threads/multithreaded.

Most helpful comment

Hi @papyr, I will update with a more complete example, but current still working. Here full example:

```C#
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}

public class Product
{
public int Id { get; set; }
public string Description { get; set; }
public double Price { get; set; }
}

// DbRef to cross references
public class Order
{
public ObjectId Id { get; set; }
public DateTime OrderDate { get; set; }
public Customer Customer { get; set; }
public List Products { get; set; }
}

public void DbRef_For_Cross_References()
{
// Re-use mapper from global instance
var mapper = BsonMapper.Global;

// Produts and Customer are other collections (not embedded document)
// you can use [BsonRef("colname")] attribute
mapper.Entity<Order>()
    .DbRef(x => x.Products, "products")
    .DbRef(x => x.Customer, "customers");

using (var db = new LiteDatabase("@MyData.db"))
{
    var customers = db.GetCollection<Customer>("customers");
    var products = db.GetCollection<Product>("products");
    var orders = db.GetCollection<Order>("orders");

    // create examples
    var john = new Customer { Name = "John Doe" };
    var tv = new Product { Description = "TV Sony 44\"", Price = 799 };
    var iphone = new Product { Description = "iPhone X", Price = 999 };
    var order1 = new Order { OrderDate = new DateTime(2017, 1, 1), Customer = john, Products = new List<Product>() { iphone, tv } };
    var order2 = new Order { OrderDate = new DateTime(2017, 10, 1), Customer = john, Products = new List<Product>() { iphone } };

    // insert into collections
    customers.Insert(john);
    products.Insert(new Product[] { tv, iphone });
    orders.Insert(new Order[] { order1, order2 });

    // create index in OrderDate
    orders.EnsureIndex(x => x.OrderDate);

    // When query Order, includes references
    var query = orders
        .Include(x => x.Customer)
        .Include(x => x.Products)
        .Find(x => x.OrderDate == new DateTime(2017, 1, 1));

    // Each instance of Order will load Customer/Products references
    foreach(var c in query)
    {
        Console.WriteLine("#{0} - {1}", c.Id, c.Customer.Name);

        foreach(var p in c.Products)
        {
            Console.WriteLine(" > {0} - {1:c}", p.Description, p.Price);
        }
    }
}

}

```

All 2 comments

Hi @papyr, I will update with a more complete example, but current still working. Here full example:

```C#
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}

public class Product
{
public int Id { get; set; }
public string Description { get; set; }
public double Price { get; set; }
}

// DbRef to cross references
public class Order
{
public ObjectId Id { get; set; }
public DateTime OrderDate { get; set; }
public Customer Customer { get; set; }
public List Products { get; set; }
}

public void DbRef_For_Cross_References()
{
// Re-use mapper from global instance
var mapper = BsonMapper.Global;

// Produts and Customer are other collections (not embedded document)
// you can use [BsonRef("colname")] attribute
mapper.Entity<Order>()
    .DbRef(x => x.Products, "products")
    .DbRef(x => x.Customer, "customers");

using (var db = new LiteDatabase("@MyData.db"))
{
    var customers = db.GetCollection<Customer>("customers");
    var products = db.GetCollection<Product>("products");
    var orders = db.GetCollection<Order>("orders");

    // create examples
    var john = new Customer { Name = "John Doe" };
    var tv = new Product { Description = "TV Sony 44\"", Price = 799 };
    var iphone = new Product { Description = "iPhone X", Price = 999 };
    var order1 = new Order { OrderDate = new DateTime(2017, 1, 1), Customer = john, Products = new List<Product>() { iphone, tv } };
    var order2 = new Order { OrderDate = new DateTime(2017, 10, 1), Customer = john, Products = new List<Product>() { iphone } };

    // insert into collections
    customers.Insert(john);
    products.Insert(new Product[] { tv, iphone });
    orders.Insert(new Order[] { order1, order2 });

    // create index in OrderDate
    orders.EnsureIndex(x => x.OrderDate);

    // When query Order, includes references
    var query = orders
        .Include(x => x.Customer)
        .Include(x => x.Products)
        .Find(x => x.OrderDate == new DateTime(2017, 1, 1));

    // Each instance of Order will load Customer/Products references
    foreach(var c in query)
    {
        Console.WriteLine("#{0} - {1}", c.Id, c.Customer.Name);

        foreach(var p in c.Products)
        {
            Console.WriteLine(" > {0} - {1:c}", p.Description, p.Price);
        }
    }
}

}

```

Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

darcome picture darcome  路  3Comments

lidanger picture lidanger  路  3Comments

ghiboz picture ghiboz  路  4Comments

muhamad picture muhamad  路  3Comments

nightroman picture nightroman  路  3Comments