c# – 使用mySql在Entity Frameworking中使用Decimal DataType无法更新行

我一直在研究这个问题太久了.我的所有select和insert命令都可以正常工作,但是当涉及到更新十进制列时,我会收到错误.

我使用以下软件

> ASP.Net v4
> MySQL Connector Net 6.3.3
> MySql Server 5.0.51a

产品类别

public class Product()
{
    public int ProductID {get;set;}
    public string Name {get;set;}
    public decimal Price {get;set;}
    public string Description {get;set;}
}

代码产生错误

var context = ObjectContextHelper.CurrentObjectContext;

var item = GetProductByID(ProductID);

if (!context.IsAttached(item))
    context.Product.Attach(item);

item.Barcode = Barcode;
item.Price = Price;
item.ProductID = ProductID;
item.Name = Name;
item.Description = Description;

context.SaveChanges();

数据库架构

CREATE TABLE `product` (
  `ProductID` int(10) unsigned NOT NULL auto_increment,
  `Name` varchar(45) character set latin1 default NULL,
  `Description` text character set latin1,
  `Price` decimal(10,2) default NULL,
  PRIMARY KEY  (`ProductID`)
) ENGINE=MyISAM AUTO_INCREMENT=154 DEFAULT CHARSET=utf8 PACK_KEYS=1$$

我收到的内部异常错误是

InnerException = {"The specified value is not an instance of type 'Edm.Int64'\r\nParameter name: value"}

如果停止更新价格列,则不会触发错误.

这是实体映射

    <?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="Wombat.Store" Alias="Self" Provider="MySql.Data.MySqlClient" ProviderManifestToken="5.0" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
        <EntityContainer Name="WombatStoreContainer">
          <EntitySet Name="product" EntityType="Wombat.Store.Product" store:Type="Tables" Schema="charlees" />

        </EntityContainer>
        <EntityType Name="product">
          <Key>
            <PropertyRef Name="ProductID" />
          </Key>
          <Property Name="Price" Type="decimal" Scale="2" />
          <Property Name="ProductID" Type="uint" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="Name" Type="varchar" MaxLength="45" />
          <Property Name="Description" Type="text" />

        </EntityType>

        <Function Name="isNullDecimal" ReturnType="decimal" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="charlees" />
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Wombat.Commerce.Data" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
        <EntityContainer Name="WombatEntities" annotation:LazyLoadingEnabled="true">

          <EntitySet Name="Products" EntityType="Wombat.Commerce.Data.Product" />

        </EntityContainer>

        <EntityType Name="Product">
          <Key>
            <PropertyRef Name="ProductID" />
          </Key>

          <Property Type="Decimal" Name="Price" Nullable="true" />

          <Property Type="Int32" Name="ProductID" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Type="String" Name="Name" />
          <Property Type="String" Name="ShortDescription" />
          <Property Type="String" Name="Sku" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs" Space="C-S">
        <Alias Key="Model" Value="Wombat" />
        <Alias Key="Target" Value="Wombat.Store" />
        <EntityContainerMapping CdmEntityContainer="WombatEntities" StorageEntityContainer="WombatStoreContainer">
          <EntitySetMapping Name="Products">
            <EntityTypeMapping TypeName="Wombat.Commerce.Data.Product">
              <MappingFragment StoreEntitySet="product">
                <ScalarProperty Name="Description" ColumnName="Description" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="ProductID" ColumnName="ProductID" />
                <ScalarProperty Name="Price" ColumnName="Price" />

              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>

        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>


</edmx:Edmx>

最佳答案 这似乎与您的问题类似.

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/1e40d986-4e5c-4da1-a526-b8cf472fb4d5

您必须创建Int64类型的包装器属性.

点赞