Error in creating a controller file - entity-framework

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.

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.

Code First one to many relationship and category with parent category

public class Category
{
[Key]
public int CategoryId { get; set; }
public int ParentCategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Status { get; set; }
public virtual ICollection<Category> ParentCategories { get; set; }
public virtual ICollection<ImageSet> ImageSets { get; set; }
[ForeignKey("ParentCategoryId")]
public virtual Category ParentCategory { get; set; }
}
public class ImageSet
{
[Key]
public int ImageSetId { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string InsertDate { get; set; }
public int Status { get; set; }
public virtual ICollection<Image> Images { get; set; }
[ForeignKey("CategoryId")]
public virtual Category Category { get; set; }
}
public class Image
{
[Key]
public int ImageId { get; set; }
public int ImageSetId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public string ThumbImageUrl { get; set; }
public string InsertDate { get; set; }
public int Status { get; set; }
[ForeignKey("ImageSetId")]
public virtual ImageSet ImageSet { get; set; }
}
Context:
public DbSet<Category> Categories { get; set; }
public DbSet<Image> Images { get; set; }
public DbSet<ImageSet> ImageSets { get; set; }
error page:Introducing FOREIGN KEY constraint 'FK_dbo.ImageSets_dbo.Categories_CategoryId' on table 'ImageSets' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could
not create constraint. See previous errors.
Whats the problem?
You need to add this:
modelBuilder.Entity<ImageSet>()
.HasRequired(is => is.Category)
.WithMany(c => c.ImageSets)
.WillCascadeOnDelete(false);
Here are good explanations of why this is happening :
https://stackoverflow.com/a/19390016/1845408
https://stackoverflow.com/a/17127512/1845408

how to get student courses with 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)

The column name is not valid

I get a common error that probably is easy to solve but I have no clue how. This is the message I get.
The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if any) = Extent1,Column name = Blog_BlogId ]
This is my classes representing my entities:
public class BlogContext : DbContext
{
public BlogContext()
: base("SqlCeServices")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
}
[Table("aspnet_Users")]
public class User
{
[Required]
public Guid ApplicationId { get; set; }
[Required]
public Guid UserId { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string LoweredUserName { get; set; }
public string MobileAlias { get; set; }
[Required]
public bool IsAnonymous { get; set; }
[Required]
public DateTime LastActivityDate { get; set; }
}
public class Blog
{
[Key]
public int BlogId { get; set; }
[Required]
public string Name { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
public virtual ICollection<User> Editors { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
[Key]
public int PostId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public string Abstract { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateLastEdited { get; set; }
public virtual User UserId { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual Blog BlogId { get; set; }
}
public class Category
{
[Key]
public int CategoryId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public string Description { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual Blog BlogId { get; set; }
}
public class Tag
{
[Key]
public int TagId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public virtual Blog BlogId { get; set; }
}
Consider changing your Post class so that BlogId references the FK to the associated Blog instead of the Blog object itself. Add a new property Blog to reference the actual Blog object.
public class Post
{
[Key]
public int PostId { get; set; }
[Required, MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public string Abstract { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateLastEdited { get; set; }
public virtual User UserId { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
You can try...ForeignKey annotation
public virtual int BlogId { get; set; }
[ForeignKey("BlogId ")]
public virtual Blog Blog { get; set; }
Detach the connection from DB, Delete any App Data related to application and rebuild and try again, It worked for me.