locked
There was an error running the selected code generator: Unable to retrieve metadata for a model RRS feed

  • Question

  • User-1330485181 posted

    Hi All,

    I want to create a login page in  ASP.NET MVC program, there is an error like this below :

    There was an error running the selected code generator:
    'Unable to retrieve metadata for 'IDXData.Models.GetMs_UserModel', One or more validation error were detected during model generation:

    GetMs_UserModel::Entity 'GetMs_UserModel' has no key defined. Define the key for this EntityType.
    GetMs_UserModel: EntityType: EntitySet 'GetMs_UserModel' is based on type 'GetMs_UserModel' that has no keys defined.

    I have created a model named GetMs_UserModel.cs

    using System.Web;
    using System.ComponentModel.DataAnnotations;
    
    namespace IDXData.Models
    {
        public class GetMs_UserModel
        {
            [Required(ErrorMessage = "UserId is required")]
            public string Userid { get; set; }
            [Required(ErrorMessage = "Password is required")]
            [DataType(DataType.Password)]
            public string userpassword { get; set; }
        }
    }

    My ms_user table is like this below :

    CREATE TABLE [dbo].[ms_user](
    	[userid] [varchar](15) NOT NULL,
    	[username] [varchar](25) NULL,
    	[userpswd] [varchar](50) NOT NULL,
    	[lastlogindate] [datetime] NULL,
    	[loginfailedcount] [int] NULL,
    	[loginipaddress] [varchar](25) NULL,
    	[lastupdate] [datetime] NULL,
    	[updater] [varchar](20) NULL,
    	[statusrec] [char](1) NULL,
    	[laston] [date] NULL,
     CONSTRAINT [PK_ms_user] PRIMARY KEY CLUSTERED 
    (
    	[userid] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    with a store procedure like this below :
    CREATE PROCEDURE [dbo].[GetMs_User]
    	-- Add the parameters for the stored procedure here
    	@userid			varchar(15), 
    	@userpassword	varchar(50)
    AS
    SET NOCOUNT ON  
    Declare @Failedcount AS INT  
    SET @Failedcount = (SELECT loginfailedcount from ms_user WHERE userid = @userid)  
      
    IF EXISTS(SELECT * FROM ms_user WHERE userid = @userid)  
      
     BEGIN  
      
     IF EXISTS(SELECT * FROM ms_user WHERE userid = @userid AND userpswd = @userpassword )  
        SELECT 'Success' AS UserExists  
    ELSE  
      
    Update ms_user set  loginfailedcount = @Failedcount+1  WHERE userid = @userid 
      
    Update ms_user set lastLogindate=GETDATE()  WHERE userid = @userid 
     BEGIN  
    IF @Failedcount >=6  
      
      
    SELECT 'Maximum Attempt Reached (6 times) .Your Account is locked now.' AS UserExists  
    ELSE  
      
    select CONVERT(varchar(10), (SELECT loginfailedcount from ms_user   WHERE userid = @userid))   AS UserFailedcount  
    END   
    END  
     ELSE  
      
     BEGIN   
      
    SELECT 'User Does not Exists' AS UserExists  
     END  
    
    GO
    
    When I tried to create a LoginController I got an error as mentioned above. How to solve this problem ?
    Thanks.

    Regards,

    Sentoso

    Monday, May 11, 2020 3:57 PM

Answers

  • User-474980206 posted

    in EF very entity needs a primary key defined. while a column named ID will default to a key, if UserId is to be the primary key add the attribute. As scaffolding does not look at the database (only the model), defining a key in the database only is not useful. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 5:14 PM

All replies

  • User-474980206 posted

    in EF very entity needs a primary key defined. while a column named ID will default to a key, if UserId is to be the primary key add the attribute. As scaffolding does not look at the database (only the model), defining a key in the database only is not useful. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 5:14 PM
  • User-1330485181 posted

    Thank Bruce for the advice.  I add the attribute for the Class GetMs_UserModel

    [Key]
    [Column(Order = 0)]

    It is working.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace IDXData.Models
    {
        public class GetMs_UserModel
        {
            [Key]
            [Column(Order = 0)]
            [Required(ErrorMessage = "UserId is required")]
            public string userid { get; set; }
    
            [Required(ErrorMessage = "Password is required")]
            [DataType(DataType.Password)]
            public string userpassword { get; set; }
        }
    }

    Tuesday, May 12, 2020 12:42 AM