Skip to main content
 首页 » 编程设计

c#之错误 40 edmx 文件 MVC : The Type decimal(18, 0) 未使用命名空间或别名限定

2024年08月06日32jirigala

我对 MVC 概念很陌生。我正在使用 Microsoft Visual Studio Express 2013 for Web 和 SQL Server LocalDb v11。到目前为止,我还没有遇到任何问题。但后来我将数据库中的价格字段从实数更改为十进制 (18,0)。我收到有关 edmx 文件的错误:

Error 40: The Type decimal(18,0) is not qualified with a namespace or alias. Only primitive types can be used without qualification. 

我已经尝试在我的 edmx 文件中使用“decimal”类型,也尝试使用 int(只是为了看看它是否有效),但仍然出现相同的错误。

我感谢每一个答案。

数据库部分:

CREATE TABLE [dbo].[Item] ( 
    [Id]        INT            NOT NULL, 
    [DepartmentId] INT            NOT NULL, 
    [Name]       NVARCHAR (50)  NOT NULL, 
    [Description]      NVARCHAR (MAX) NOT NULL, 
    [Price]      DECIMAL           NOT NULL, 
    [Quantity]  INT            NOT NULL, 
    [AuthorId]   INT            NULL, 
    PRIMARY KEY CLUSTERED ([Id] ASC), 
    CONSTRAINT [FK_Item_ToDepartment] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([Id]), 
    CONSTRAINT [FK_Item_ToAuthor] FOREIGN KEY ([AuthorId]) REFERENCES [dbo].[Author] ([Id]) 
); 

模型类:

       namespace WebStore.Models 
 
{ 
    using System; 
    using System.Collections.Generic; 
 
    public partial class Item 
    { 
        public Item() 
        { 
            this.Cart = new HashSet<Cart>(); 
        } 
 
        public int Id { get; set; } 
        public int DepartmentId { get; set; } 
        public string Name { get; set; } 
        public string Description { get; set; } 
        public decimal Price { get; set; } 
        public int Quantity { get; set; } 
        public Nullable<int> AuthorId { get; set; } 
 
        public virtual Author Author { get; set; } 
        public virtual Department Department { get; set; } 
        public virtual ICollection<Cart> Cart { get; set; } 
    } 
}     

以及 edmx 文件中出现错误 40 的部分:

<EntityType Name="Item"> 
      <Key> 
        <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="Int32" Nullable="false" /> 
      <Property Name="DepartmentId" Type="Int32" Nullable="false" /> 
      <Property Name="Name" Type="String" MaxLength="50" FixedLength="false" Unicode="true" Nullable="false" /> 
      <Property Name="Description" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" Nullable="false" /> 
      <Property Name="Price" Type="decimal(18,0)" Nullable="false" /> 
      <Property Name="Quantity" Type="Int32" Nullable="false" /> 
      <Property Name="AuthorId" Type="Int32" /> 
      <NavigationProperty Name="Author" Relationship="Self.FK_Item_ToAuthor" FromRole="Izdelek" ToRole="Author" /> 
      <NavigationProperty Name="Department" Relationship="Self.FK_Item_ToDepartment" FromRole="Izdelek" ToRole="Departmetn" /> 
      <NavigationProperty Name="Cart" Relationship="Self.FK_Cart_ToItem" FromRole="Izdelek" ToRole="Cart" /> 
    </EntityType>     

请您参考如下方法:

如果您使用十进制,请尝试使用大写 D 来指定类型,例如 Type="Decimal" Precision="18" Scale="0"在edmx:ConceptualModels edmx 文件的一部分。 edmx:StorageModels Type="decimal" Precision="18" Scale="0" 部分应该没问题.