{"action":"opened","issue":{"url":"https://api.github.com/repos/mbdavid/LiteDB/issues/460","repository_url":"https://api.github.com/repos/mbdavid/LiteDB","labels_url":"https://api.github.com/repos/mbdavid/LiteDB/issues/460/labels{/name}","comments_url":"https://api.github.com/repos/mbdavid/LiteDB/issues/460/comments","events_url":"https://api.github.com/repos/mbdavid/LiteDB/issues/460/events","html_url":"https://github.com/mbdavid/LiteDB/issues/460","id":205462656,"number":460,"title":"Question : How can i insert records","user":{"login":"aytacasan","id":640271,"avatar_url":"https://avatars.githubusercontent.com/u/640271?v=3","gravatar_id":"","url":"https://api.github.com/users/aytacasan","html_url":"https://github.com/aytacasan","followers_url":"https://api.github.com/users/aytacasan/followers","following_url":"https://api.github.com/users/aytacasan/following{/other_user}","gists_url":"https://api.github.com/users/aytacasan/gists{/gist_id}","starred_url":"https://api.github.com/users/aytacasan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aytacasan/subscriptions","organizations_url":"https://api.github.com/users/aytacasan/orgs","repos_url":"https://api.github.com/users/aytacasan/repos","events_url":"https://api.github.com/users/aytacasan/events{/privacy}","received_events_url":"https://api.github.com/users/aytacasan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-02-05T23:12:34Z","updated_at":"2017-02-05T23:12:34Z","closed_at":null,"body":"Hi @mbdavid,\r\n\r\nI think, i understand all concepts about document database. Because of that i start to use LiteDB in my project. I implement my simple entities first insert, update etc working fine. But i'm stuck about my complex entities. I produce sample console application which is below. I tried many ways for insert entities to db but i received an exception in every situation. Could you please help me about that problem.\r\n\r\n* Am i missing something? Because every exception from LiteDB is null reference exception and they don't have any inner exception because of that i don't understand what is the problem. Is there a way to catch inner exceptions?\r\n* How can i save my TradingHours entity in one step?\r\n* If not possible one step insert how can i save simply?\r\n\r\n```\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Globalization;\r\nusing LiteDB;\r\n\r\nnamespace LiteDBTest\r\n{\r\n    public class TradingHours\r\n    {\r\n        public TradingHours()\r\n        {\r\n\r\n        }\r\n\r\n        public TradingHours(string name, TimeZoneInfo timeZoneInfo)\r\n            : this()\r\n        {\r\n            Name = name;\r\n            TimeZoneInfo = timeZoneInfo;\r\n        }\r\n\r\n        public TradingHours(string name, TimeZoneInfo timeZoneInfo, IEnumerable<Session> sessions, IEnumerable<Holiday> holidays)\r\n            : this(name, timeZoneInfo)\r\n        {\r\n            Sessions = sessions;\r\n            Holidays = holidays;\r\n        }\r\n\r\n        public int Id { get; set; }\r\n        public string Name { get; set; }\r\n        public TimeZoneInfo TimeZoneInfo { get; set; }\r\n        public IEnumerable<Session> Sessions { get; set; }\r\n        public IEnumerable<Holiday> Holidays { get; set; }\r\n    }\r\n\r\n    public class Session\r\n    {\r\n        public Session()\r\n        {\r\n\r\n        }\r\n\r\n        public Session(TradingHours tradingHours)\r\n            : this()\r\n        {\r\n            TradingHours = tradingHours;\r\n        }\r\n\r\n        public Session(TradingHours tradingHours, DayOfWeek startDay, TimeSpan startTime, DayOfWeek endDay, TimeSpan endTime, bool endOfDay)\r\n            : this(tradingHours)\r\n        {\r\n            StartDay = startDay;\r\n            StartTime = startTime;\r\n            EndDay = endDay;\r\n            EndTime = endTime;\r\n            EndOfDay = endOfDay;\r\n        }\r\n\r\n        public int Id { get; set; }\r\n        public TradingHours TradingHours { get; set; }\r\n        public DayOfWeek StartDay { get; set; }\r\n        public TimeSpan StartTime { get; set; }\r\n        public DayOfWeek EndDay { get; set; }\r\n        public TimeSpan EndTime { get; set; }\r\n        public bool EndOfDay { get; set; }\r\n    }\r\n\r\n    public class Holiday\r\n    {\r\n        public Holiday()\r\n        {\r\n\r\n        }\r\n\r\n        public Holiday(TradingHours tradingHours)\r\n            : this()\r\n        {\r\n            TradingHours = tradingHours;\r\n        }\r\n\r\n        public Holiday(TradingHours tradingHours, string name, HolidayType type, DateTime date, TimeSpan startTime, TimeSpan endTime)\r\n            : this(tradingHours)\r\n        {\r\n            Name = name;\r\n            Type = type;\r\n            Date = date;\r\n            StartTime = startTime;\r\n            EndTime = endTime;\r\n        }\r\n\r\n        public int Id { get; set; }\r\n        public TradingHours TradingHours { get; set; }\r\n        public string Name { get; set; }\r\n        public HolidayType Type { get; set; }\r\n        public DateTime Date { get; set; }\r\n        public TimeSpan StartTime { get; set; }\r\n        public TimeSpan EndTime { get; set; }\r\n    }\r\n\r\n    public enum HolidayType\r\n    {\r\n        FullDay,\r\n        LateOpen,\r\n        EarlyClose\r\n    }\r\n\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            BsonMapper.Global.Entity<TradingHours>()\r\n                .Id(e => e.Id)\r\n                .Index(e => e.Name, true)\r\n                .DbRef(e => e.Sessions, \"Sessions\")\r\n                .DbRef(e => e.Holidays, \"Holidays\");\r\n\r\n            BsonMapper.Global.Entity<Session>()\r\n                .Id(e => e.Id)\r\n                .Index(e => e.TradingHours)\r\n                .DbRef(e => e.TradingHours, \"TradingHours\");\r\n\r\n            BsonMapper.Global.Entity<Holiday>()\r\n                .Id(e => e.Id)\r\n                .Index(e => e.TradingHours)\r\n                .DbRef(e => e.TradingHours, \"TradingHours\");\r\n\r\n            BsonMapper.Global.RegisterType(\r\n                (timeSpan) => timeSpan.Ticks,\r\n                (bsonValue) => TimeSpan.FromTicks(bsonValue.AsInt64));\r\n\r\n            BsonMapper.Global.RegisterType(\r\n                (cultureInfo) => cultureInfo.Name,\r\n                (bsonValue) => CultureInfo.GetCultureInfo(bsonValue.AsString));\r\n\r\n            BsonMapper.Global.RegisterType(\r\n                (timeZoneInfo) => timeZoneInfo.Id,\r\n                (bsonValue) => TimeZoneInfo.FindSystemTimeZoneById(bsonValue.AsString));\r\n\r\n            using (var database = new LiteDatabase(AppDomain.CurrentDomain.BaseDirectory + \"Data.db\"))\r\n            {\r\n                var tradingHoursCollection = database.GetCollection<TradingHours>(\"TradingHours\");\r\n                var sessionCollection = database.GetCollection<Session>(\"Sessions\");\r\n                var holidayCollection = database.GetCollection<Holiday>(\"Holidays\");\r\n\r\n                if (tradingHoursCollection.FindOne(tradingHours => tradingHours.Name == \"US Equities Regular\") == null)\r\n                {\r\n                    var tradingHours = new TradingHours(\"US Equities Regular\", TimeZoneInfo.FindSystemTimeZoneById(\"Eastern Standard Time\"));\r\n\r\n                    var sessions = new List<Session>\r\n                    {\r\n                        new Session(tradingHours, DayOfWeek.Monday, new TimeSpan(9, 30, 0), DayOfWeek.Monday, new TimeSpan(16, 0, 0), true),\r\n                        new Session(tradingHours, DayOfWeek.Tuesday, new TimeSpan(9, 30, 0), DayOfWeek.Tuesday, new TimeSpan(16, 0, 0), true),\r\n                        new Session(tradingHours, DayOfWeek.Wednesday, new TimeSpan(9, 30, 0), DayOfWeek.Wednesday, new TimeSpan(16, 0, 0), true),\r\n                        new Session(tradingHours, DayOfWeek.Thursday, new TimeSpan(9, 30, 0), DayOfWeek.Thursday, new TimeSpan(16, 0, 0), true),\r\n                        new Session(tradingHours, DayOfWeek.Friday, new TimeSpan(9, 30, 0), DayOfWeek.Friday, new TimeSpan(16, 0, 0), true)\r\n                    };\r\n\r\n                    var holidays = new List<Holiday>\r\n                    {\r\n                        new Holiday(tradingHours, \"New Year's Day\", HolidayType.FullDay, new DateTime(2016, 1, 1), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"Martin Luther King, Jr. Day\", HolidayType.FullDay, new DateTime(2016, 1, 18), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"President's Day\", HolidayType.FullDay, new DateTime(2016, 2, 15), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"Good Friday\", HolidayType.FullDay, new DateTime(2016, 3, 25), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"Memorial Day\", HolidayType.FullDay, new DateTime(2016, 5, 30), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"Independence Day\", HolidayType.FullDay, new DateTime(2016, 7, 4), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"Labor Day\", HolidayType.FullDay, new DateTime(2016, 9, 5), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"Thanksgiving\", HolidayType.FullDay, new DateTime(2016, 11, 24), TimeSpan.Zero, TimeSpan.Zero),\r\n                        new Holiday(tradingHours, \"Thanksgiving\", HolidayType.EarlyClose, new DateTime(2016, 11, 25), TimeSpan.Zero, new TimeSpan(13, 0, 0)),\r\n                        new Holiday(tradingHours, \"Christmas Day\", HolidayType.FullDay, new DateTime(2016, 12, 15), TimeSpan.Zero, TimeSpan.Zero)\r\n                    };\r\n\r\n                    using (var transaction = database.BeginTrans())\r\n                    {\r\n                        try\r\n                        {\r\n                            tradingHours.Sessions = new List<Session>(sessions);\r\n                            tradingHours.Holidays = new List<Holiday>(holidays);\r\n                            tradingHoursCollection.Insert(tradingHours);\r\n                            transaction.Commit();\r\n                        }\r\n                        catch (Exception)\r\n                        {\r\n                            transaction.Rollback();\r\n                            throw;\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n```"}}
{"actor":{"display_login":"aytacasan"}}