2010-08-25 28 views
4

我一直在研究这个问题太久。我所有的选择和插入命令都能正常工作,但是当涉及到更新小数列时,我会遇到错误。使用mySql在实体框架中使用十进制数据类型更新行

我使用以下软件

  • 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"} 

误差不火,如果你停止更新到Price Col UMN。

这是实体映射

<?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> 
+0

我会问,MySQL有Int32而不是Int64 - 只需更改edmx文件中的int类型即可。 – 2010-08-25 22:40:18

+0

虽然我看不到它的设置为int64。我附上了edmx文件的内容 – Damon 2010-08-25 23:44:31

回答