how to get student courses with entity framework - entity-framework

I have 2 classes, course and student, and I want to get student courses with student id
help me please
I'm using entity framework code first
public class Course {
public int CourseID { get; set; }
public string Title { get; set; }
public string Details { get; set; }
public string img { get; set; }
public DateTime DateAdded { get; set; }
public Guid UserAddID { get; set; }
public Guid userEditID { get; set; }
public int SubCategoryID { get; set; }
public string Code { get; set; }
public string Summury { get; set; }
public decimal Price { get; set; }
public bool IsGift { get; set; }
public DateTime DateUpdated { get; set; }
public bool DelFlag { get; set; }
public DateTime? DelDate { get; set; }
public Guid? UserDelID { get; set; }
public ICollection<Student> Students { get; set; }
}
public class Student {
[Key]
public int StudentID { get; set; }
public string Name { get; set; }
public DateTime? DateBirth { get; set; }
public int? Age { get; set; }
public int? Sex { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Mobile2 { get; set; }
public string UniversityID { get; set; }
public string CollegeID { get; set; }
public string IdentityID { get; set; }
public string Address { get; set; }
public string Education { get; set; }
public decimal? Payments { get; set; }
public string HowToKnow { get; set; }
public DateTime? GradeDate { get; set; }
public string LaterCourses { get; set; }
public string Job { get; set; }
public string Specialist { get; set; }
public string Notes { get; set; }
public int StatueID { get; set; }
public Guid? UserID { get; set; }
public DateTime? DateAdded { get; set; }
public Guid UserAddID { get; set; }
public Guid userEditID { get; set; }
public DateTime DateUpdated { get; set; }
public bool DelFlag { get; set; }
public DateTime? DelDate { get; set; }
public Guid? UserDelID { get; set; }
public virtual IList<Course> Courses { get; set; }
}
I want to get all courses for a given StudentID using a lambda expression.

var students = repository.Query<Course>(c => c.Student.Any(st=>st.StudentID == StudentID)

Related

Adding an entity in EF Core

I am using EF Core db first approach, .NET6. I have the following classes:
public class Doctor
{
[Key]
public int DoctorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Specialization { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Designation { get; set; }
public bool IsDeleted { get; set; }
}
and
public class Patient
{
[Key]
public int PatientId { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string NIC { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public DateTime DOB { get; set; }
public string Gender { get; set; }
public string Reference { get; set; }
public short SerialNumberYear { get; set; }
public int SerialNumber { get; set; }
[Required]
public int DoctorId { get; set; } //need to assign Primary doc. Is this correct?
public Doctor Doctor { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public string CreatedBy { get; set; }
public List<Doctor> SecondaryDoctors { get; set; }//need to assign a list of secondary docs. is this correct?
public bool SMS_Allowed { get; set; }
public bool Email_Allowed { get; set; }
public string SpecialConcern1 { get; set; }
public string SpecialConcern2 { get; set; }
public string SpecialConcern3 { get; set; }
public string SpecialConcern4 { get; set; }
}
The Patient class needs to have a Primary doctor assigned and a list of Secondary doctors assigned. What entries should I make in the Patient class to accomodate this requirement? I tried the entries shown above with comments. Is that correct? When I add a patient with this code, I get the following error when creating a new patient record:
Duplicate entry '1' for key 'doctors.PRIMARY'
So when creating a patient, why is efcore trying to create a doctor record?

Code first one-to-many relationship with restraints

I have the following one-to-many database structure generated by code first. Many Buttons can be added with a relation back to a single GSMUnit. BtnNo should be unique (for every GSMUnitId), and shouldn't be duplicated. Currently it is possible to duplicate. how can i prevent this?
public class GSMUnit
{
public int Id { get; set; }
[MaxLength(20)]
public string Model { get; set; }
[MaxLength(40)]
public string GsmName { get; set; }
[MaxLength(40)]
public string TelephoneNum { get; set; }
public int GSMSiteId { get; set; }
public virtual GSMSite GSMSite { get; set; }
public virtual GSMSetting GSMSettings { get; set; }
public virtual ICollection<Button> Buttons { get; set; }
}
public class Button
{
public int Id { get; set; }
[MaxLength(30)]
public string Primary { get; set; }
[MaxLength(30)]
public string Divert1 { get; set; }
[MaxLength(30)]
public string Divert2 { get; set; }
[MaxLength(30)]
public string Divert3 { get; set; }
[MaxLength(20)]
public string AptNo { get; set; }
[MaxLength(10)]
public string Code { get; set; }
[MaxLength(4)]
public string DTO { get; set; }
[MaxLength(4)]
public string Timeband { get; set; }
[MaxLength(15)]
public string BtnNo { get; set; }
[MaxLength(20)]
public string AptName { get; set; }
public int GSMUnitId { get; set; }
public virtual GSMUnit GSMUnit { get; set; }
}
You can use the Index attribute for that:
public class Button
{
public int Id { get; set; }
[MaxLength(30)]
public string Primary { get; set; }
[MaxLength(30)]
public string Divert1 { get; set; }
[MaxLength(30)]
public string Divert2 { get; set; }
[MaxLength(30)]
public string Divert3 { get; set; }
[MaxLength(20)]
public string AptNo { get; set; }
[MaxLength(10)]
public string Code { get; set; }
[MaxLength(4)]
public string DTO { get; set; }
[MaxLength(4)]
public string Timeband { get; set; }
[MaxLength(15)]
public string BtnNo { get; set; }
[MaxLength(20)]
public string AptName { get; set; }
[Index(IsUnique = true)]
public int GSMUnitId { get; set; }
public virtual GSMUnit GSMUnit { get; set; }
}
But I recommend you to take a look on Fluent API. It's easier to configure in my opinion.

Why does EF.core sometimes includes invalid columns?

How does EF.Core choose it's columns?
I have these two simple classes: Payment and Site
I use this for my select list:
var payments = db.Payments
.Include(e => e.Site)
.AsNoTracking();
EF.Core generates this invalid sql:
SELECT [e].[pay_id], [e].[amount], [e].[cheque_no], [e].[client_id], [e].[dt], [e].[last_modified], [e].[last_modified_by], [e].[pay_dt], [e].[pay_mode], [e].[site_id], [e.Site].[site_id], [e.Site].[address], [e.Site].[client_id], [e.Site].[last_modified], [e.Site].[last_modified_by], [e.Site].[name], [e.Site].[state]
FROM [Payments] AS [e]
INNER JOIN [Sites] AS [e.Site] ON [e].[site_id] = [e.Site].[site_id]
ORDER BY [e.Site].[name]
There is no client_id in the Payments table or Payment class below
public class Payment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int32 pay_id { get; set; }
public DateTime? dt { get; set; }
public Int32 site_id { get; set; }
public Decimal? amount { get; set; }
public DateTime? pay_dt { get; set; }
public String pay_mode { get; set; }
public String cheque_no { get; set; }
public virtual Site Site { get; set; }
public DateTime? last_modified { get; set; }
public int? last_modified_by { get; set; }
}
public class Site
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int32 site_id { get; set; }
public String name { get; set; }
public Int32 client_id { get; set; }
public String address { get; set; }
public String state { get; set; }
public virtual Client Client { get; set; }
public List<Deployment> Deployments { get; set; }
public List<Contract> Contracts { get; set; }
public List<Timesheet> Timesheets { get; set; }
public DateTime? last_modified { get; set; }
public int? last_modified_by { get; set; }
}

Error in creating a controller file

I am using Entity Framework. I have tried everything, searching and adding keys but Ienter image description here cannot understand what the problem is and how to resolve it.
public class Reservation
{
[Key]
public int BookingID { get; set; }
public int CustomerID { get; set; }
public int RoomID { get; set; }
public string BookingDate { get; set; }
public int Check_In { get; set; }
public int Check_Out { get; set; }
public int Adults { get; set; }
public int Children { get; set; }
public int NoOfNights { get; set; }
[ForeignKey("RoomID")]
public virtual Room Rooms { get; set; }
[ForeignKey("CustomerID")]
public virtual CustomerDetails CustomerDetail { get; set; }
public virtual ICollection<Payment> Payment { get; set; }
}
public class CustomerDetails
{
[Key]
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int PostCode { get; set; }
public string State { get; set; }
public int PhoneNumber { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual ICollection<Reservation> Reservations { get; set; }
}
enter image description here
All tables need a primary key or you can't use Entity Framework.

Invalid Columns using EF

Can someone please help me with this problem? I am new to EF and i don't know what happen to this code:
Please find below query in the Controller:
foreach (var i in tt)
{
db.tbl_transaction.Add(i);
db.tbl_transactionlist.Add(tm.tlist);
var tools = (from j in db.tbl_tools_and_equips where j.itemno == i.controlno select j).First();
//tools.location = i.destinationid;
//tools.linemodel = i.line_model;
//tools.usage = i.process_assign;
tools.status = i.status;
tools.date_maintain = i.date_maintain;
tools.date_next_maintain = i.date_next_maintain;
}
try
{
/*return error: invalid columns in db.SaveChanges*/
db.SaveChanges();
For my Model:
public partial class tbl_transaction
{
public int transferevent { get; set; }
public string transactionno { get; set; }
public Nullable<int> itemno { get; set; }
public string assetdesc { get; set; }
public string serialno { get; set; }
public string barcode { get; set; }
public string sourcelocationid { get; set; }
public string destinationid { get; set; }
public Nullable<System.DateTime> transactiondate { get; set; }
public Nullable<int> quantity { get; set; }
public string unitmeasure { get; set; }
public string remarks { get; set; }
public string #ref { get; set; }
public string actg_rem { get; set; }
public string label { get; set; }
public string station { get; set; }
public string line_model { get; set; }
public string process_assign { get; set; }
public string status { get; set; }
public string controlno { get; set; }
public string category { get; set; }
public Nullable<System.DateTime> date_maintain { get; set; }
public Nullable<System.DateTime> date_repair { get; set; }
public string defect { get; set; }
public string repair_made { get; set; }
public Nullable<System.DateTime> date_next_maintain { get; set; }
}
Every time I execute the query, it always return invalid column name. Your help is very much appreciated.