entity framework insert bug

Posted by tmfkmoney on Stack Overflow See other posts from Stack Overflow or by tmfkmoney
Published on 2010-03-27T17:14:22Z Indexed on 2010/03/27 17:23 UTC
Read the original article Hit count: 230

Filed under:
|
|

I found a previous question which seemed related but there's no resolution and it's 5 months old so I've opened my own version.

http://stackoverflow.com/questions/1545583/entity-framework-inserting-new-entity-via-objectcontext-does-not-use-existing-e

When I insert records into my database with the following it works fine for a while and then eventually it starts inserting null values in the referenced field. This typically happens after I do an update on my model from the database although not always after I do an update. I'm using a MySQL database for this.

I have debugged the code and the values are being set properly before the save event. They're just not getting inserted properly. I can always fix this issue by re-creating the model without touching any of my code. I have to recreate the entire model, though. I can't just dump the relevant tables and re-add them. This makes me think it doesn't have anything to do with my code but something with the entity framework. Does anyone else have this problem and/or solved it?

 using (var db = new MyModel())
        {
            var stocks = from record in query
                         let ticker = record.Ticker
                         select new
                                   {
                                       company = db.Companies.FirstOrDefault(c => c.ticker == ticker),
                                       price = Convert.ToDecimal(record.Price),
                                       date_stamp = Convert.ToDateTime(record.DateTime)
                                   };

                foreach (var stock in stocks)
                {
                    if (stock.company != null) 
                    {
                        var price = new StockPrice
                                        {
                                            Company = stock.company,
                                            price = stock.price,
                                            date_stamp = stock.date_stamp
                                        };

                        db.AddToStockPrices(price);
                    }
                }

                db.SaveChanges();
        }

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about bugs