Entity Framework 6 - Configuration mapping from multiple tables - entity-framework

I have two tables which looks like below:
http://s30.postimg.org/i3u7pmpz5/baza.png
I created models looks like below:
public class DomainUser : User
{
public Guid Guid { get; set; }
public string Sid { get; set; }
public string Title { get; set; }
public string Company { get; set; }
public string Department { get; set; }
public string Office { get; set; }
public virtual DomainPhone Phone { get; set; }
public virtual DomainEmail Email { get; set; }
public string Created { get; set; }
public string CreatedBy { get; set; }
public string Modified { get; set; }
public string ModifiedBy { get; set; }
}
public abstract class User
{
public int Id { get; set; }
public string Login { get; set; }
public string Name { get; set; }
public string MiddleName { get; set; }
public string Surname { get; set; }
public bool IsActive { get; set; }
}
I want to create DomainUser object with data from table DomainUser and User.
Here is a sample query which we want to generate by EF:
select [Extent1].[Id] as [Id],
[Extent1].[Login] as [Login],
[Extent2].[Guid] as [Guid],
[Extent2].[Sid] as [Sid],
[Extent2].[Title] as [Title],
[Extent2].[Company] as [Company],
[Extent2].[Department] as [Department],
[Extent2].[Office] as [Office]
from [dbo].[User] as [Extent1] inner join [dbo].[DomainUser] as [Extent2] on [Extent1].[Id] = [Extent2].[Id]
How I can achieve this?

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?

Identity Core - Relation between IdentityUser table and other tables

How to create a one-to-many relation between IdentityUser (auto generated Table) and other tables?
in my situation i have users table and want to add a one-to-many relation to Address table, and add userId and userEmail as rows to the Address table.
user table
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Address> Addresses { get; set; }
}
addresses table
public class Address
{
public int ID { get; set; }
public string AddressName { get; set; }
public string Country { get; set; }
public string FullName { get; set; }
public string MobileNumber { get; set; }
public string FirstDetails { get; set; }
public string SecondDetails { get; set; }
public string BuildingNumber { get; set; }
public string CompanyName { get; set; }
public string StreetName { get; set; }
public string Area { get; set; }
public string City { get; set; }
public int PostalCode { get; set; }
public string NearestLandmark { get; set; }
public ApplicationUser User { get; set; }
}

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; }
}

EF Core code first Inheritance of separate table

Say I have a table Company defined in following entity:
public class Company
{
public Guid CompanyId { get; set; }
[Required]
public string Name { get; set; }
[MaxLength(50)]
public string Uid { get; set; }
...
}
And I need another table CompanyHistory what will have all fields of Company extended with CompanyHistoryId, EffectiveDate, DEffectiveDate.
I have tried it like this:
public class CompanyHistory : Company
{
public Guid CompanyHistoryId { get; set; }
public virtual Company { get; set; }
}
But instead of 2 tables migration makes one and combines all the columns.
How can I get same result without writing all the column again as is done here:
public class CompanyHistory
{
public Guid CompanyHistoryId { get; set; }
public Guid CompanyId { get; set; }
public virtual Company Company { get; set; }
[Required]
public string Name { get; set; }
[MaxLength(50)]
public string Uid { get; set; }
...
}

Entity Framework 4.3 CF many - to - many relationship saving object?

When creating many to many relationship using EF 4.3 code first approach, I cannot save data to connecting table, also cannot any examples on how to fill this table using saving object to Icollection... Here is my example:
MODELS
public class Hospital
{
//PK
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string County { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Guid User_Id { get; set; }
//FK
public virtual ICollection<Operator> Operators { get; set; }
}
public class Operator
{
//PK
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
public string Email { get; set; }
//FK
public virtual ICollection<Hospital> Hospitals { get; set; }
}
public class Project: DbContext
{
public DbSet<Hospital> Hospitals { get; set; }
public DbSet<Operator> Operators { get; set; }
}
CONTROLLER
public void AddOperater()
{
Hospital h = new Hospital();
h = db.Hospitals.Single(a=>a.Id ==1);
var o = new Operator();
o.FirstName = "John";
o.LastName = "Doe";
o.Dob = new DateTime(1988,2,12);
o.Email = "johndoe#gmail.com";
o.Hospitals.Add(h);
db.SaveChanges();
}
With this approach I keep getting error here: o.Hospitals.Add(h); even when my Hospital instance is filled with data. How exactly to save data to both tables, the dbo.Operators and dbo.OperatorHospital which is relationship table?
o.Hospitals.Add(h) will fail because the list is a null list. You cannot call Add() on a null list. Typically most people get around this by instantiating the list in the constructor of the entity... like so... the current line is blowing up due to a CSharp issue.
public class Hospital
{
//PK
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string County { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Guid User_Id { get; set; }
//FK
public virtual ICollection<Operator> Operators { get; set; }
public Hospital()
{
Operators = new List<Operator>();
}
}
public class Operator
{
//PK
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
public string Email { get; set; }
//FK
public virtual ICollection<Hospital> Hospitals { get; set; }
public Operator()
{
Hospitals = new List<Hospital>();
}
}