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:
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
ModelGetRestrictionsDD
Method with EF coding in.
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";
}
}