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

标签 c# mysql linq entity-framework

我已经在这个问题上工作了太久。我的所有选择和插入命令都可以正常工作,但在更新十进制列时出现错误。

我正在使用以下软件

  • ASP.Net v4
  • MySQL 连接器网络 6.3.3
  • MySql 服务器 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 类型的包装器属性。

关于c# - 无法使用 mySql 在 Entity Frameworking 中使用 Decimal DataType 更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3570766/

相关文章:

asp.net-mvc - ASP.NET 使用 LINQ 处理 DB 表单

c# - Excel.exe 应用程序在使用完后未关闭

c# - asp.net excel插件

php - 使用日期格式搜索一段时间内的数据

php - Solr 搜索不适用于 BOTH LIKE

linq - 如何在 LINQ 中按多个字段排序?

c# - Linq 包含对字符串的检查有误检测

c# - 这真的是节省分配吗?

MySQL TAdoConnnection 连接 "connected"属性错误设置为 True

c# - FirstOrDefault 返回意外值