question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Insert complex objects

See original GitHub issue

Hi, I want to insters complex object to database. Can you verify what I’m doing wronmg?

public class Phone
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Number { get; set; }
        }
public class Customer
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<Phone> Phones { get; set; }
            public bool IsActive { get; set; }
        }

        static void Main(string[] args)
        {
            using (var db = new LiteDatabase(@"C:\MyData.db"))
            {

                var phones = db.GetCollection<Phone>("phones");
                var customers = db.GetCollection<Customer>("customers");

                BsonMapper.Global.Entity<Customer>()
                    .Id(e => e.Id)
                    .DbRef(e => e.Phones, "phones");

                var customer = new Customer
                {
                    Name = "Dawid",
                    Phones = new List<Phone> { new Phone { Name = " damien", Number = "111222" }, new Phone { Name = " dawid", Number = "555666" } },
                    IsActive = true
                };

                customers.Insert(customer);

                // this list have 1 object, but in this object Phones has Id = 0 and another properties are null
                var allCustomers = customers.Include(c => c.Phones).FindAll().ToList();

                // this list is empty
                var allPhones = phones.FindAll().ToList();
            }
        }

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
rikimaru0345commented, Nov 24, 2017

If there are any concepts or ideas you have that you want feedback on (maybe if you forgot something or just want to get an outside opinion for proofchecking) let us know 😃

Maybe we can also help implementing it if you want…

3reactions
rikimaru0345commented, Nov 24, 2017

Why does it not insert reference objects automatically? It seems like that should be a standard feature…

Also, an option to automatically include references.

BsonMapper.Global.Entity<Customer>() .Id(e => e.Id) .DbRef(e => e.Phones, “phones”, alwaysAutomaticallyInclude : true, alwaysAutoInsert: true)

This should exist. Automatic loading of references, and automatic inserting when a parent object is inserted. (When inserting Customer, all Phone entries are inserted/updated as well).

Why not add this? It would be a great feature to have. @mbdavid

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity framework - how to add complex objects to db
I'm confused how to add new objects to database, which has two one to many foreign keys. For example how to properly add...
Read more >
How do you add complex objects in EF Core
How do you add complex objects in EF Core - A new object containing a collection of new and existing items. Hey guys,....
Read more >
Inserting Complex objects into Collections in MongoDB - Part 10
MongoDB Tutorial for Beginners - Inserting Complex objects into Collections in MongoDB - Part 10 · Comments2.
Read more >
How to Bulk Insert Complex Objects into SQL Server ...
First column shows number of objects that were inserted, with average of two EmailAddress subobjects per SiteUser object. Second column is the ...
Read more >
Bulk insert C# complex objects into SQL Server ... - YouTube
This video illustrates how we can write C# code to bulk insert user defined objects into a SQL Server Table using SqlBulkCopy class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found