locked
System.Data.Entity.Core.MappingException, "Namespace.Enum' in conceptual side cannot be mapped to type 'System.Enum' on the object side. Both the types must be abstract or both must be concrete types. RRS feed

  • Question

  • I am getting System.Data.Entity.Core.MappingException, "Namespace.Enum' in conceptual side cannot be mapped to type 'System.Enum' on the object side.  Both the types must be abstract or both must be concrete types.

    What I am doing

    1. Create a model class based on existing database structure. 

    2. Create a custom class that derived from dbcontext. The custom class takes connection string as parameters.

    The custom class has a property of dbset<model> 

    3. In my controller call custom class and try to return dbset<model> to view. 

    custom connection class

    namespace customNameSpace
    {
        public class customDbConnection : DbContext
        {
            public customDbConnection (string connection) : base(connection)
            {
            }
            public DbSet<user_menu> user_menus{ get; set; }
          }
    }
     

    data model 

        public class user_menu {
            [Key]
            public int id { get; set; }
            public string menu_category { get; set; }
            public string menu_link { get; set; }
            public string menu_item { get; set; }
            public int role_id { get; set; }
    
        }
    
    


    action 

     public ActionResult Index()
            {
                var db = new customDbConnection("connection"); 
    
    //there is an entry in webconfig 
    
                List<user_menu> test = db.user_menus.Where(um=> um.Id ==1).ToList();
    
                return View(test);
            }

    I have been searching online but couldn't figure out what is missing here. 

    Things I have tried:

    • reconstruct the db model class
    • reconstruct the custom dbcontext class
    • delete bin/obj folder, clean solution and rebuild.

    Hopefully someone here could shed some lights on this error. 

    Thanks.


    Sunday, October 21, 2018 10:33 PM

Answers

  • Hi Zhanglong,

    Thanks for replying. I actually found the issue myself after spending quite a bit of time. 

    The issue is caused by a typo in one of the database model. I accidentally set one of the property type to be enum. 

    My dbcontext returns multiple dbsets and the problematic one is not the one I want to use in this scenario. 

    I only find out the issue after a search against enum keyword against the whole project. Thankfully the project is still relatively small as I only started it a week ago. 

    Kind Regards,

    Ryan 

    Wednesday, October 24, 2018 12:21 AM

All replies

  • Hi Ryan89520,

    Based on your description and related code, I create a simple sample with entity framework 6.2.0 & LocalDB, it works as except, table struct as below.

    CREATE TABLE [dbo].[user_menu] (
        [id]            INT            IDENTITY (1, 1) NOT NULL,
        [menu_category] NVARCHAR (MAX) NULL,
        [menu_link]     NVARCHAR (MAX) NULL,
        [menu_item]     NVARCHAR (MAX) NULL,
        [role_id]       INT            NOT NULL
    );
    Could you please share a simple sample that could reproduce the issue via OneDrive.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 22, 2018 6:57 AM
  • Hi Zhanglong,

    Thanks for replying. I actually found the issue myself after spending quite a bit of time. 

    The issue is caused by a typo in one of the database model. I accidentally set one of the property type to be enum. 

    My dbcontext returns multiple dbsets and the problematic one is not the one I want to use in this scenario. 

    I only find out the issue after a search against enum keyword against the whole project. Thankfully the project is still relatively small as I only started it a week ago. 

    Kind Regards,

    Ryan 

    Wednesday, October 24, 2018 12:21 AM
  • Hi Ryan,

    I am glad to know that you resolve the issue and thanks for sharing the solution to us, please mark it as answer, it will be beneficial to other communities who have similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 24, 2018 1:07 AM