views:

29

answers:

0

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();
        }

Here are parts of my entity framework.

<EntityType Name="company">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="active_down" Type="int" />
          <Property Name="active_up" Type="int" />
          <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
      <Property Name="market_id" Type="int" Nullable="false" />
      <Property Name="name" Type="varchar" Nullable="false" MaxLength="100" />
      <Property Name="rating" Type="decimal" Precision="4" Scale="2" />
      <Property Name="ticker" Type="varchar" Nullable="false" MaxLength="8" />
      <Property Name="total_down" Type="int" />
      <Property Name="total_up" Type="int" />
    </EntityType>

<EntityType Name="StockPrice">
          <Key>
            <PropertyRef Name="id" />
          </Key>
          <Property Name="date_stamp" Type="DateTime" Nullable="false" />
          <Property Name="id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="price" Type="Decimal" Nullable="false" />
          <NavigationProperty Name="Company" Relationship="MyDb.CompanyStockPrice" FromRole="StockPrice" ToRole="Company" />
        </EntityType>
        <Association Name="CompanyStockPrice">
          <End Type="MyDb.Company" Role="Company" Multiplicity="1" />
          <End Type="MyDb.StockPrice" Role="StockPrice" Multiplicity="*" />
        </Association>

 <AssociationSet Name="CompanyStockPrice" Association="MyDb.CompanyStockPrice">
            <End Role="Company" EntitySet="Companies" />
            <End Role="StockPrice" EntitySet="StockPrices" />
          </AssociationSet>