EF query Error and unsure how to resolve it

capbiker

New member
Joined
Feb 23, 2020
Messages
3
Programming Experience
5-10
I am building a room booking system in C#, and I am having an issue surrounding an EF query. When I am trying to build the application it keeps on returning the following error:

The entity or complex type 'Room_Booking_System.Room_Controls.Room_Model.ModelGetRestrictionDD' cannot be constructed in a LINQ to Entities query.

I have been trying to resolve it, and haven't had any success. The coding all looks the same to the rest of the EF queries that I have written. And I am a bit stump at the problem.

Here are the different class that I am using.

ModelREstrictionsControl
C#:
namespace Room_Booking_System.Room_Controls.Room_Model
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class ModelRestrictionsControl : DbContext
    {
        public ModelRestrictionsControl()
            : base("name=ModelRestrictionsControl")
        {
        }

        public virtual DbSet<ModelGetRestrictionDD> tblRestrictions { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ModelGetRestrictionDD>()
                .Property(e => e.RestrictionLevel)
                .IsUnicode(false);
        }
    }
}

ModelGetRestrictionsDD
C#:
namespace Room_Booking_System.Room_Controls.Room_Model
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class ModelGetRestrictionDD
    {
        [Key]
        public int RestrictionId { get; set; }

        [Required]
        public string RestrictionLevel { get; set; }

        public bool RestrictionActive { get; set; }
    }
}

Method with EF coding in.
C#:
        private void getRestrictionLevels()
        {
                using (ModelRestrictionsControl restriction = new ModelRestrictionsControl())
                {
                    List<ModelGetRestrictionDD> rLevel = new List<ModelGetRestrictionDD>();

                    rLevel = restriction.tblRestrictions
                        .Where(r => r.RestrictionActive == true)
                        .Select(r => new ModelGetRestrictionDD { RestrictionId = r.RestrictionId, RestrictionLevel = r.RestrictionLevel, RestrictionActive = r.RestrictionActive })
                        .OrderBy(r => new { r.RestrictionId }).ToList();

                    cboRestrictionLevel.DataSource = rLevel;
                    cboRestrictionLevel.DisplayMember = "RestrictionLevel";
                    cboRestrictionLevel.ValueMember = "RestrictionId";

                }
        }
 
Hello

No, this is the first time that I have used that entity to link to that table.

What I did was created the entity/model. Wrote up the entity query and then tried to run it. And it returned the above error.

Had it been a conflict with another query, I could have understood it.
 

Latest posts

Back
Top Bottom