Insert complex objects
See original GitHub issueHi, 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:
- Created 6 years ago
- Comments:10 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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…
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