views:

509

answers:

2

I have an insert method on my repository like so:

public T Insert(T entity) 
{
    _ctx.AddObject(EntityName, entity);
    _ctx.SaveChanges();
    return entity;
}

If I execute the below code, the values assigned to my entity do not propagate to the SQL that is executed.

Category c = new Category();
c.Name = CLEARANCE;
c = categoryManager.Insert(c);

The SQL should be something like

INSERT INTO Category(Name) VALUES('Clearance')

Instead, the following SQL is being executed

insert [dbo].[Category]([Name])
values (null)
select [Id]
from [dbo].[Category]
where @@ROWCOUNT > 0 and [Id] = scope_identity()

I debugged the code right down to the AddObject method and verified that the Name property was set on the entity, but it doesn't reflect that in the SQL.

Can you see anything wrong with what I'm doing?


Updated with mapping xml

    <?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"&gt;
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="ProductCatalogModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl"&gt;
        <EntityContainer Name="ProductCatalogModelStoreContainer">
          <EntitySet Name="Category" EntityType="ProductCatalogModel.Store.Category" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="Product" EntityType="ProductCatalogModel.Store.Product" store:Type="Tables" Schema="dbo" />
          <AssociationSet Name="FK_Product_Category" Association="ProductCatalogModel.Store.FK_Product_Category">
            <End Role="Category" EntitySet="Category" />
            <End Role="Product" EntitySet="Product" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="Category">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
        </EntityType>
        <EntityType Name="Product">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
          <Property Name="CategoryId" Type="int" Nullable="false" />
        </EntityType>
        <Association Name="FK_Product_Category">
          <End Role="Category" Type="ProductCatalogModel.Store.Category" Multiplicity="1" />
          <End Role="Product" Type="ProductCatalogModel.Store.Product" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Category">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Product">
              <PropertyRef Name="CategoryId" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="ProductCatalogModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm"&gt;
        <EntityContainer Name="ProductCatalogEntities">
          <EntitySet Name="Category" EntityType="ProductCatalogModel.Category" />
          <EntitySet Name="Product" EntityType="ProductCatalogModel.Product" />
          <AssociationSet Name="FK_Product_Category" Association="ProductCatalogModel.FK_Product_Category">
            <End Role="Category" EntitySet="Category" />
            <End Role="Product" EntitySet="Product" />
          </AssociationSet>
          </EntityContainer>
        <EntityType Name="Category">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Int32" Nullable="false" />
          <Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
          <NavigationProperty Name="Product" Relationship="ProductCatalogModel.FK_Product_Category" FromRole="Category" ToRole="Product" />
        </EntityType>
        <EntityType Name="Product" Abstract="false">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Int32" Nullable="false" />
          <Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
          <NavigationProperty Name="Category" Relationship="ProductCatalogModel.FK_Product_Category" FromRole="Product" ToRole="Category" />
        </EntityType>
        <Association Name="FK_Product_Category">
          <End Role="Category" Type="ProductCatalogModel.Category" Multiplicity="1" />
          <End Role="Product" Type="ProductCatalogModel.Product" Multiplicity="*" />
        </Association>
        </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
        <EntityContainerMapping StorageEntityContainer="ProductCatalogModelStoreContainer" CdmEntityContainer="ProductCatalogEntities">
          <EntitySetMapping Name="Category">
            <EntityTypeMapping TypeName="IsTypeOf(ProductCatalogModel.Category)">
              <MappingFragment StoreEntitySet="Category">
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="Name" ColumnName="Name" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="Product">
            <EntityTypeMapping TypeName="IsTypeOf(ProductCatalogModel.Product)">
              <MappingFragment StoreEntitySet="Product">
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="Name" ColumnName="Name" />
              </MappingFragment>
            </EntityTypeMapping>
            </EntitySetMapping>
          <AssociationSetMapping Name="FK_Product_Category" TypeName="ProductCatalogModel.FK_Product_Category" StoreEntitySet="Product">
            <EndProperty Name="Category">
              <ScalarProperty Name="Id" ColumnName="CategoryId" />
            </EndProperty>
            <EndProperty Name="Product">
              <ScalarProperty Name="Id" ColumnName="Id" />
            </EndProperty>
          </AssociationSetMapping>
          </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx"&gt;
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </edmx:Connection>
    <edmx:Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
      </DesignerInfoPropertySet>
    </edmx:Options>
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram Name="Model">
        <EntityTypeShape EntityType="ProductCatalogModel.Category" Width="1.5" PointX="0.75" PointY="0.875" Height="1.427958984375" IsExpanded="true" />
        <EntityTypeShape EntityType="ProductCatalogModel.Product" Width="1.5" PointX="3" PointY="0.875" Height="1.427958984375" IsExpanded="true" />
        <AssociationConnector Association="ProductCatalogModel.FK_Product_Category" ManuallyRouted="false">
          <ConnectorPoint PointX="2.25" PointY="1.5889794921875" />
          <ConnectorPoint PointX="3" PointY="1.5889794921875" /></AssociationConnector>
        </Diagram></edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>
+1  A: 

I hate to say this, but all I did was mess with it for about an hour and now it works. I did not change a freaking thing. Thanks Microsoft for such a production ready technology.

Brian
Come to think of it. I'm pretty sure I've had this experience. Only in dev though. I've had an app running for about a month no issues in production.
Michael Gattuso
A: 

Probably not related to this issue but a couple of things to think about with EntityFramework

  1. The compilation is database specific. Problems can arise if you don't compile it against the database version you plan on using (eg. SQL Server 2005 vs 2000) - this can cause issues if your local DB is 2005 vs a test or production enviroment is 2000.

  2. Making a small change to the edmx file, even something as small as moving an entity in the designer view will cause the database mapping files to recompile and can possibly fix issues related to mapping / etc.

Michael Gattuso