Creating Entity Model with Cross Referencing Table - c#

Here are my models:
public partial class NEWS
{
public NEWS()
{
}
[Key]
public int NEWSID { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string InsertDate { get; set; }
public int GroupingID { get; set; }
public virtual Subjects Subjects { get; set; }
}
public partial class Subjects
{
public Subjects()
{ this.NEWSs = new HashSet<NEWS>(); }
[Key]
public int GroupingID { get; set; }
public string Farsi { get; set; }
public string Latin { get; set; }
public virtual ICollection<NEWS> NEWSs { get; set; }
}
public class UserGroup
{
public UserGroup()
{ this.Userss = new HashSet<Users>(); }
[Key]
public int UserID { get; set; }
public string Title { get; set; }
public virtual ICollection<Users> Userss { get; set; }
}
public class Users
{
public Users()
{ }
public string Name { get; set; }
public string Family { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public string PassWord { get; set; }
[Key]
public int UserID { get; set; }
public virtual UserGroup UserGroup { get; set; }
// public HashSet<Users> Userss { get; set; }
}
public class NEWSDBContext : DbContext
{
public NEWSDBContext()
: base()
{
Database.SetInitializer<NEWSDBContext>(null);
}
public DbSet<NEWS> NEWSs { get; set; }
public DbSet<Users> Userss { get; set; }
public DbSet<UserGroup> UserGroups { get; set; }
public DbSet<Subjects> Subjectss { get; set; }
}
I always get an error in return View(newss.ToList());:
The underlying provider failed on Open

Related

Get All data from two tables in .net 5 web api?

Patient.cs //This is Patient Model Class
namespace HMS.Models
{
public class Patient
{
[Key]
public string Id { get; set; }
public string Name { get; set; }
public int age { get; set; }
public int Weight { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public string PhoneNo { get; set; }
public string Disease { get; set; }
[JsonIgnore]
public IList<DoctorPatient> DoctorPatients { get; set; }
public InPatient InPatients { get; set; }
public OutPatient OutPatients { get; set; }
}
}
InPatient.cs //This InPatient Model Class
namespace HMS.Models
{
public class InPatient
{
[ForeignKey("Patient")]
public string InPatientId { get; set; }
public string RoomNo { get; set; }
public DateTime DateOfAddmission { get; set; }
public DateTime DateOfDischarge { get; set; }
public int Advance { get; set; }
public string LabNo { get; set; }
public Patient Patient { get; set; }
}
}
Here Patient and InPatient Attribute have one-to-one relationship
ViewInPatient.cs
namespace HMS.Models
{
public class ViewInPatient
{
public string Name { get; set; }
public int age { get; set; }
public int Weight { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public string PhoneNo { get; set; }
public string Disease { get; set; }
public string RoomNo { get; set; }
public DateTime DateOfAddmission { get; set; }
public DateTime DateOfDischarge { get; set; }
public int Advance { get; set; }
public string LabNo { get; set; }
}
}
Here is my DbContext class
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DoctorPatient>()
.HasOne(x => x.Doctor)
.WithMany(dp => dp.DoctorPatients)
.HasForeignKey(di => di.DoctorId);
modelBuilder.Entity<DoctorPatient>()
.HasOne(y => y.Patient)
.WithMany(dp => dp.DoctorPatients)
.HasForeignKey(pi => pi.PatientId);
}
public DbSet<Patient> Patients { get; set; }
public DbSet<Doctor> Doctors { get; set; }
public DbSet<DoctorPatient> DoctorPatients { get; set; }
public DbSet<InPatient> InPatients { get; set; }
//public DbQuery<ViewInPatient> ViewInPatients { get; set; }
}
How to get all data of both Patients and InPatients Table like in ViewInPatient class? (I tried to create a view in sql server but in add table window it shows InPatient instead of InPatients and it return null value)
You can join both models in a Linq expression and return ViewInPatient list:
var ViewInPatient_set =
YourContext
.InPatients
.Select(i=> new ViewInPatient()
{
Name = i.Patient.Name,
// ...
RoomNo = i.RoomNo,
// ...
}
)
.ToList(); // <-- transform to list is optional

AutoMapperMappingException: Missing type map configuration or unsupported mapping

I'm attempting to use AutoMapper for the first time. The GSMSite maps fine, but I get an error when trying to map the GSMUnit:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Do I need to specify a relationship between GSMSite and GSMUnits?
Domain class:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public string Telephone { get; set; }
public bool PasswordReset { get; set; } = false;
public virtual ICollection<GSMSite> GSMSites { get; set; }
}
public class GSMSite
{
public int Id { get; set; }
public string SiteName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactName { get; set; }
public string ContactNumber { get; set; }
public string ContactEmail { get; set; }
public string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual ICollection<GSMUnit> GSMUnits { get; set; }
}
public class GSMUnit
{
public int Id { get; set; }
public string Model { get; set; }
public string Firmware { get; set; }
public string TelephoneNum { get; set; }
public int GSMSiteId { get; set; }
public virtual GSMSite GSMSite { get; set; }
}
Contract class:
public class GSMResponse
{
public int Id { get; set; }
public string Model { get; set; }
public string Firmware { get; set; }
public string TelephoneNum { get; set; }
}
public class SiteResponse
{
public int Id { get; set; }
public string SiteName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactName { get; set; }
public string ContactNumber { get; set; }
public string ContactEmail { get; set; }
}
Mapping:
public DomainToResponseProfile()
{
CreateMap<GSMSite, SiteResponse>();
CreateMap<GSMUnit, GSMResponse>();
}

How to add a list of objects inside a list in C# MVC

I have a view model containing list of sections like below. I need to create a list of ResponseEntryViewModel and add sections and sub sections inside sections and questions inside subsections.
Any suggestions?
public class ResponseEntryViewModel
{
public int TypeID { get; set; }
public string TypeName { get; set; }
public int User_ID { get; set; }
public List<SectionDataModel> Sections{ get; set; }
public ResponseEntryViewModel()
{
Sections = new List<SectionDataModel>();
}
public class SectionDataModel
{
public int SectionID { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public int TypeId { get; set; }
public List<SubSectionModel> SubSections { get; set; }
public SectionDataModel()
{
SubSections = new List<SubSectionModel>();
}
}
public class SubSectionModel
{
public int SubSectionID { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public int SectionId { get; set; }
public List<QuestionModel> QuestionsList { get; set; }
public SubSectionModel()
{
QuestionsList = new List<QuestionModel>();
}
}
public class QuestionModel
{
public int SubSectionID { get; set; }
public int QuestionID { get; set; }
public string Question { get; set; }
}
}
Try this:
public class ResponseEntryViewModel
{
public int TypeID { get; set; }
public string TypeName { get; set; }
public int User_ID { get; set; }
public List<SectionDataModel> Sections { get; set; }
public ResponseEntryViewModel(SectionDataModel obj)
{
Sections = new List<SectionDataModel>();
Sections.Add(obj);
}
public class SectionDataModel
{
public int SectionID { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public int TypeId { get; set; }
public List<SubSectionModel> SubSections { get; set; }
public SectionDataModel(SubSectionModel obj)
{
SubSections = new List<SubSectionModel>();
SubSections.Add(obj);
}
}
public class SubSectionModel
{
public int SubSectionID { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public int SectionId { get; set; }
public List<QuestionModel> QuestionsList { get; set; }
public SubSectionModel(QuestionModel obj)
{
QuestionsList = new List<QuestionModel>();
QuestionsList.Add(obj);
}
}
public class QuestionModel
{
public int SubSectionID { get; set; }
public int QuestionID { get; set; }
public string Question { get; set; }
}
}

How to parse Json from the RootObject?

I am trying to parse JSON response so I created some classes.
Actually I want Leg and Flight class element value. I am trying to get those element value from RootObject but I don't know how to do this. I googled but I am little bit confuse.
I paste my JSON respose , Classes !!
JSON Response :
http://pastebin.com/fjjLxkd2
Classes :
public class Detail
{
}
public class Airport
{
public string kind { get; set; }
public string code { get; set; }
public string city { get; set; }
public string name { get; set; }
}
public class City
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Aircraft
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Tax
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class Carrier
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Data
{
public string kind { get; set; }
public List<Airport> airport { get; set; }
public List<City> city { get; set; }
public List<Aircraft> aircraft { get; set; }
public List<Tax> tax { get; set; }
public List<Carrier> carrier { get; set; }
}
public class Flight
{
public string carrier { get; set; }
public string number { get; set; }
}
public class Leg
{
public string kind { get; set; }
public string id { get; set; }
public string aircraft { get; set; }
public string arrivalTime { get; set; }
public string departureTime { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string originTerminal { get; set; }
public int duration { get; set; }
public int onTimePerformance { get; set; }
public int mileage { get; set; }
public string meal { get; set; }
public bool secure { get; set; }
public string destinationTerminal { get; set; }
public string operatingDisclosure { get; set; }
}
public class Segment
{
public string kind { get; set; }
public int duration { get; set; }
public Flight flight { get; set; }
public string id { get; set; }
public string cabin { get; set; }
public string bookingCode { get; set; }
public int bookingCodeCount { get; set; }
public string marriedSegmentGroup { get; set; }
public List<Leg> leg { get; set; }
public int connectionDuration { get; set; }
}
public class Slouse
{
public string kind { get; set; }
public int duration { get; set; }
public List<Segment> segment { get; set; }
}
public class Fare
{
public string kind { get; set; }
public string id { get; set; }
public string carrier { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string basisCode { get; set; }
}
public class BagDescriptor
{
public string kind { get; set; }
public string commercialName { get; set; }
public int count { get; set; }
public string subcode { get; set; }
public List<string> description { get; set; }
}
public class FreeBaggageOption
{
public string kind { get; set; }
public List<BagDescriptor> bagDescriptor { get; set; }
public int pieces { get; set; }
}
public class SegmentPricing
{
public string kind { get; set; }
public string fareId { get; set; }
public string segmentId { get; set; }
public List<FreeBaggageOption> freeBaggageOption { get; set; }
}
public class Passengers
{
public string kind { get; set; }
public int adultCount { get; set; }
}
public class Tax2
{
public string kind { get; set; }
public string id { get; set; }
public string chargeType { get; set; }
public string code { get; set; }
public string country { get; set; }
public string salePrice { get; set; }
}
public class Pricing
{
public string kind { get; set; }
public List<Fare> fare { get; set; }
public List<SegmentPricing> segmentPricing { get; set; }
public string baseFareTotal { get; set; }
public string saleFareTotal { get; set; }
public string saleTaxTotal { get; set; }
public string saleTotal { get; set; }
public Passengers passengers { get; set; }
public List<Tax2> tax { get; set; }
public string fareCalculation { get; set; }
public string latestTicketingTime { get; set; }
public string ptc { get; set; }
}
public class TripOption
{
public string kind { get; set; }
public string saleTotal { get; set; }
public string id { get; set; }
public List<Slouse> slice { get; set; }
public List<Pricing> pricing { get; set; }
}
public class Trips
{
public string kind { get; set; }
public string requestId { get; set; }
public Data data { get; set; }
public List<TripOption> tripOption { get; set; }
}
public class RootObject
{
public string kind { get; set; }
public Trips trips { get; set; }
}
Code :
var obj0 = JsonConvert.DeserializeObject<RootObject>(responsedata);
Here I got only Trip class element . I want the Leg and Flight class element.
If you want to find information for a specific leg, you need to use Linq to traverse the tree. For example, if you just know the leg id, you could do something like this:
var allLegs = obj0.trips.tripOption.SelectMany(to => to.slice.SelectMany(sl => sl.segment.Select(sg => sg.leg)));
var leg = allLegs.FirstOrDefault(l => l.id == "yourId");
The query for retrieving a flight would be similar. You could also filter by tripOption id to get a specific tripOption and then retrieve flights or legs that are associated with it.

Cannot update multiple tables though no error occurs

I have following parent class which has multiple child classes. I have successfully created and retrieved data from SQL database. Now I want to update record by using FormCollection.
public class UserDetailContext : DbContext
{
public UserDetailContext()
: base("DefaultConnection")
{
}
public DbSet<UserDetails> UserDetails { get; set; }
public DbSet<PersonalDetails> PersonalDetails { get; set; }
public DbSet<HoroscopeDetails> HoroscopeDetails { get; set; }
public DbSet<Expectations> Expectations { get; set; }
public DbSet<FamilyDetails> FamilyDetails { get; set; }
public DbSet<AddressDetails> AddressDetails { get; set; }
public DbSet<EducationalDetails> EducationalDetails { get; set; }
}
public class UserDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public List <PersonalDetails> PdetailsList { get; set; }
public List<HoroscopeDetails> HDetailsList { get; set; }
public List<EducationalDetails> EDetailsList { get; set; }
public List<AddressDetails> AdddetailsList { get; set; }
public List<FamilyDetails> FDetailsList { get; set; }
public List<Expectations> ExpDetailsList { get; set; }
public PersonalDetails Pdetails { get; set; }
public HoroscopeDetails HDetails { get; set; }
public EducationalDetails EDetails { get; set; }
public AddressDetails Adddetails { get; set; }
public FamilyDetails FDetails { get; set; }
public Expectations ExpDetails { get; set; }
public UserDetails()
{
Pdetails = new PersonalDetails();
HDetails = new HoroscopeDetails();
EDetails = new EducationalDetails();
Adddetails = new AddressDetails();
FDetails = new FamilyDetails();
ExpDetails = new Expectations();
}
}
public class PersonalDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 0)]
public int PersonalID { get; set; }
public int UserId { get; set; }
public string RegID { get; set; }
public string FirstName { get; set; }
public string MidName { get; set; }
public string LastName { get; set; }
}
public class HoroscopeDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Rashi { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class EducationalDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 0)]
public int EduID { get; set; }
public string EducationDetails { get; set; }
public string Education { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class AddressDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddId { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class FamilyDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int FId { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
public class Expectations
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ExpId { get; set; }
public int PersonalID { get; set; }
[ForeignKey("PersonalID")]
public virtual PersonalDetails Product { get; set; }
}
Now from controller I want to update a record
public void UpdateRecord(FormCollection coll)
{
int id = Convert.ToInt32(Session["UserId"]);
UserDetails obj = db.UserDetails.FirstOrDefault(s => s.UserId.Equals(id));
obj.Pdetails.FirstName = "xxxx";
obj.Pdetails.LastName = "xxxx";
obj.UserId = Convert.ToInt32(Session["UserId"]);
db.Entry(obj).State = EntityState.Modified;
int result = db.SaveChanges();
}
It doesn't give any error but also doesn't update any record.
And obj object also contains null values by following code:
UserDetails obj = db.UserDetails.FirstOrDefault(s => s.UserId.Equals(id));

Categories