Entity Framework Core Multiple sub objects Include / ThenInclude - c#

I'm having trouble including all the entities when using EF to query. Using dummy labels, my DB has a structure of a Family which contains a Collection of Person objects, and a House. Each Person object has a Wallet associated with them, with the Wallet having a Driver's License object. The House has three objects: a Ktichen, Den, and Garage. My Models were set up with a DB-first approach and I can provide them if need be.
But I'm having a 'Lambda expression used inside Include is not valid.' error when running this code:
public Family GetFamily(int familyId)
{
DBContext context = new DBContext();
Family family= context.Family.Where(fam=> fam.Id== famId)
.Include(fam => fam.PersonCollection)
.ThenInclude(p => p.Wallet)
.ThenInclude(w => w.License)
.Include(fam => fam.House)
.ThenInclude(h => h.Kitchen)
.Include(fam => fam.House)
.ThenInclude(h => h.Den)
.Include(fam => fam.House)
.ThenInclude(h => h.Garage)
.First();
return family
}
If I comment out the includes for the License, and two of the three rooms in the house, it runs, but otherwise it does not.
Here are all the Models
public partial class Family
{
public Family()
{
PersonCollection = new HashSet<PersonCollection>();
}
public int HouseId { get; set; }
public virtual House House { get; set; }
public virtual ICollection<PersonCollection> PersonCollection { get; set; }
}
public partial class House
{
public House()
{
Family = new HashSet<Family>();
Den = new HashSet<Den>();
}
public int Id { get; set; }
public byte KitchenId { get; set; }
public int? DenId { get; set; }
public int? GarageId { get; set; }
public virtual Kitchen Kitchen { get; set; }
public virtual Garage GarageNavigation { get; set; }
public virtual Den DenNavigation { get; set; }
public virtual ICollection<Family> Family{ get; set; }
public virtual ICollection<Den> Den { get; set; }
}
public partial class Kitchen
{
public Kitchen()
{
House = new HashSet<House>();
}
public byte Id { get; set; }
public virtual ICollection<House> House { get; set; }
}
public partial class Garage
{
public Garage()
{
House = new HashSet<House>();
}
public int Id { get; set; }
public virtual ICollection<House> House { get; set; }
}
public partial class Den
{
public Den()
{
House = new HashSet<House>();
}
public int Id { get; set; }
public virtual ICollection<House> House { get; set; }
}
public partial class Person
{
public int FamilyId{ get; set; }
public int? WalletId { get; set; }
public virtual Wallet Wallet { get; set; }
public virtual Family Family { get; set; }
}
public partial class Wallet
{
public Wallet()
{
PersonCollection = new HashSet<Person>();
}
public int Id { get; set; }
public int? DenId { get; set; }
public virtual License License { get; set; }
public virtual ICollection<Person> PersonCollection { get; set; }
}
public partial class License
{
public License()
{
Wallet = new HashSet<Wallet>();
}
public int Id { get; set; }
public virtual ICollection<Wallet> Wallet { get; set; }
}
Here's the Model Builders. I had to rename and delete a lot of things to simplify and obfuscate our code, but hopefully I kept everything that's relevant.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Family>(entity =>
{
entity.HasOne(d => d.House)
.WithMany(p => p.Family)
.HasForeignKey(d => d.FamilyId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Family_House");
});
modelBuilder.Entity<Person>(entity =>
{
entity.HasOne(d => d.Wallet)
.WithMany(p => p.Person)
.HasForeignKey(d => d.WalletId)
.HasConstraintName("FK_Person_Wallet");
entity.HasOne(d => d.Family)
.WithMany(p => p.Person)
.HasForeignKey(d => new { d.FamilyId })
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Person_Family");
});
modelBuilder.Entity<Wallet>(entity =>
{
entity.HasOne(d => d.LicenseNavigation)
.WithMany(p => p.Wallet)
.HasForeignKey(d => d.License)
.HasConstraintName("FK_Wallet_License");
});
modelBuilder.Entity<License>(entity =>
{
entity.ToTable("License", "DBO");
});
modelBuilder.Entity<House>(entity =>
{
entity.HasOne(d => d.Kitchen)
.WithMany(p => p.House)
.HasForeignKey(d => d.KitchenId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_House_Kitchen");
entity.HasOne(d => d.GarageNavigation)
.WithMany(p => p.House)
.HasForeignKey(d => d.Garage)
.HasConstraintName("FK_House_Garage");
entity.HasOne(d => d.DenNavigation)
.WithMany(p => p.House)
.HasForeignKey(d => d.Garage)
.HasConstraintName("FK_House_Garage");
});
modelBuilder.Entity<Kitchen>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedOnAdd();
});
modelBuilder.Entity<Garage>(entity =>
{
entity.Property(e => e.Description)
.IsRequired()
.HasMaxLength(50);
});
modelBuilder.Entity<Den>(entity =>
{
entity.ToTable("Den", "DBO");
entity.Property(e => e.Description)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(false);
});
}

Related

Entity Framework Core 7.0 with postgresql [duplicate]

I have an order entity, inside it contains several entities Customer, Store and others, but Entity Framework does not fill those entities. I thought the relationships were wrong but I can't find why the entity framework does not map the entities within orders.
Orders and Customers entities:
public partial class Orders
{
public Orders()
{
OrderItems = new HashSet<OrderItems>();
}
public int OrderId { get; set; }
public int? CustomerId { get; set; }
public byte OrderStatus { get; set; }
public DateTime OrderDate { get; set; }
public DateTime RequiredDate { get; set; }
public DateTime? ShippedDate { get; set; }
public int StoreId { get; set; }
public int StaffId { get; set; }
public virtual Customers Customer { get; set; }
public virtual Staffs Staff { get; set; }
public virtual Stores Store { get; set; }
public virtual ICollection<OrderItems> OrderItems { get; set; }
}
public partial class Customers
{
public Customers()
{
Orders = new HashSet<Orders>();
}
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public virtual ICollection<Orders> Orders { get; set; }
}
Entity Framework code fragment:
modelBuilder.Entity<Orders>(entity =>
{
entity.HasKey(e => e.OrderId)
.HasName("PK__orders__46596229F9C56686");
entity.ToTable("orders", "sales");
entity.Property(e => e.OrderId).HasColumnName("order_id");
entity.Property(e => e.CustomerId).HasColumnName("customer_id");
entity.Property(e => e.OrderDate)
.HasColumnName("order_date")
.HasColumnType("date");
entity.Property(e => e.OrderStatus).HasColumnName("order_status");
entity.Property(e => e.RequiredDate)
.HasColumnName("required_date")
.HasColumnType("date");
entity.Property(e => e.ShippedDate)
.HasColumnName("shipped_date")
.HasColumnType("date");
entity.Property(e => e.StaffId).HasColumnName("staff_id");
entity.Property(e => e.StoreId).HasColumnName("store_id");
entity.HasOne(d => d.Customer)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.CustomerId)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK__orders__customer__0F4872E8");
entity.HasOne(d => d.Staff)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StaffId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__orders__staff_id__1130BB5A");
entity.HasOne(d => d.Store)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StoreId)
.HasConstraintName("FK__orders__store_id__103C9721");
});
Controller code:
Response in Postman:
One of the options is to eager load them via Include:
return await _context.Orders
.Include(o => o.Customer)
.Include(o => o.Staff)
.Include(o => o.Store)
.ToListAsync()
For more info and options please check out the "Loading Related Data" doc.

The association between entities (with the key value, CreatedById:200) has been severed - Error

While I try to delete a ProjectParticipant object from a Project object and call context.SaveChangesAsync(), I get this error:
ERROR: The association between entities ‘employee’ and ‘project’ with the key value ,CreatedById:200 has been severed but the relationship is either marked as ‘required’ or is implicitly required because the foreign key is not billable of the dependent/child entity should be deleted when a required relationship is severed, then setup the relationship to use cascade deletes.
Could someone help me fix this?
Project.cs
public class Project
{
public Project()
{
ProjectParticipants = new HashSet<ProjectParticipant>();
}
public int Id { get; set; }
public int CreatedById { get; set; }
public Employee CreatedBy { get; set; }
public virtual ICollection<ProjectParticipant> ProjectParticipants { get; set; }
}
}
Employee.cs
public class Employee
{
public Employee()
{
Projects = new HashSet<Project>();
ProjectParticipants = new HashSet<ProjectParticipant>();
}
public int Id { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<ProjectParticipant> ProjectParticipants { get; set; }
}
ProjectParticipant.cs
public class ProjectParticipant
{
public ProjectParticipant()
{
}
public int Id { get; set; }
public int ProjectId { get; set; }
public virtual Project Project { get; set; }
public int EmployeeId { get; set; }
public virtual Employee Employee { get; set; }
}
ProjectDBContext.cs
public class ProjectDBContext:DBContext
{
public override OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>(entity => {
...
entity.HasOne(d => d.CreatedBy)
.WithMany(a => a.Projects)
.HasForeignKey(d => d.CreatedById)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Project_Employee_CreatedBy");
...
});
modelBuilder.Entity<ProjectParticipant>(entity => {
...
entity.HasOne(d => d.Employee)
.WithMany(a => a.ProjectParticipants)
.HasForeignKey(d => d.EmployeeId)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK_ProjectParticipants_Employee");
entity.HasOne(d => d.Project)
.WithMany(a => a.ProjectParticipants)
.HasForeignKey(d => d.ProjectId)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK_ProjectParticipants_Project");
...
});
modelBuilder.Entity<Employee>(entity => {
...
});
}
}

Why does the entity framework not map the customer entity in my order?

I have an order entity, inside it contains several entities Customer, Store and others, but Entity Framework does not fill those entities. I thought the relationships were wrong but I can't find why the entity framework does not map the entities within orders.
Orders and Customers entities:
public partial class Orders
{
public Orders()
{
OrderItems = new HashSet<OrderItems>();
}
public int OrderId { get; set; }
public int? CustomerId { get; set; }
public byte OrderStatus { get; set; }
public DateTime OrderDate { get; set; }
public DateTime RequiredDate { get; set; }
public DateTime? ShippedDate { get; set; }
public int StoreId { get; set; }
public int StaffId { get; set; }
public virtual Customers Customer { get; set; }
public virtual Staffs Staff { get; set; }
public virtual Stores Store { get; set; }
public virtual ICollection<OrderItems> OrderItems { get; set; }
}
public partial class Customers
{
public Customers()
{
Orders = new HashSet<Orders>();
}
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public virtual ICollection<Orders> Orders { get; set; }
}
Entity Framework code fragment:
modelBuilder.Entity<Orders>(entity =>
{
entity.HasKey(e => e.OrderId)
.HasName("PK__orders__46596229F9C56686");
entity.ToTable("orders", "sales");
entity.Property(e => e.OrderId).HasColumnName("order_id");
entity.Property(e => e.CustomerId).HasColumnName("customer_id");
entity.Property(e => e.OrderDate)
.HasColumnName("order_date")
.HasColumnType("date");
entity.Property(e => e.OrderStatus).HasColumnName("order_status");
entity.Property(e => e.RequiredDate)
.HasColumnName("required_date")
.HasColumnType("date");
entity.Property(e => e.ShippedDate)
.HasColumnName("shipped_date")
.HasColumnType("date");
entity.Property(e => e.StaffId).HasColumnName("staff_id");
entity.Property(e => e.StoreId).HasColumnName("store_id");
entity.HasOne(d => d.Customer)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.CustomerId)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK__orders__customer__0F4872E8");
entity.HasOne(d => d.Staff)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StaffId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__orders__staff_id__1130BB5A");
entity.HasOne(d => d.Store)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StoreId)
.HasConstraintName("FK__orders__store_id__103C9721");
});
Controller code:
Response in Postman:
One of the options is to eager load them via Include:
return await _context.Orders
.Include(o => o.Customer)
.Include(o => o.Staff)
.Include(o => o.Store)
.ToListAsync()
For more info and options please check out the "Loading Related Data" doc.

EF6 fluent one-to-one stackoverflow

I have read a lot of posts and some helped me, but I still have a stackoverflow on a one-to-one relation (between Employe and Adresse) defined with Entity framework 6 fluent.
The error appears on AdresseDTO when I create a new instance of a ViewModel wich contains some DTOs.
My plain objects
public class Personne
{
public int id { get; set; }
public string civ { get; set; }
public string nom { get; set; }
public string prenom { get; set; }
public string email { get; set; }
public string tel1 { get; set; }
public string tel2 { get; set; }
public Boolean isFournisseur { get; set; }
public Boolean isClient { get; set; }
public Boolean isDeleted { get; set; }
public virtual Adresse adresse { get; set; }
public virtual Utilisateur utilisateur { get; set; }
public Personne()
{
}
}
public class Employe : Personne
{
public virtual TEmploye typeEmploye { get; set; }
public virtual List<AffectationService> affectationServices { get; set; }
public Employe()
{
}
}
public class AffectationService
{
public int id { get; set; }
public virtual Employe employe { get; set; }
public virtual Service service { get; set; }
public virtual Droit groupe { get; set; }
public Boolean isPrincipal { get; set; }
}
public class TEmploye
{
public int id { get; set; }
public string libe { get; set; }
public TEmploye()
{
}
}
public class Adresse
{
public int id { get; set; }
public string numRue { get; set; }
public string nomRue { get; set; }
public string codePostal { get; set; }
public string ville { get; set; }
public string pays { get; set; }
public virtual Personne personne { get; set; }
public Adresse()
{
}
}
My DTOs
public class PersonneDTO
{
public int id { get; set; }
public bool isDeleted { get; set; }
public string civ { get; set; }
public string nom { get; set; }
public string prenom { get; set; }
public string email { get; set; }
public string tel1 { get; set; }
public string tel2 { get; set; }
public AdresseDTO adresse { get; set; }
public UtilisateurDTO utilisateur { get; set; }
public PersonneDTO()
{
adresse = new AdresseDTO();
utilisateur = new UtilisateurDTO();
}
}
public class EmployeDTO : PersonneDTO
{
public virtual TEmployeDTO typeEmploye { get; set; }
public virtual List<AffectationServiceDTO> affectationServices { get; set; }
public EmployeDTO()
{
affectationServices = new List<AffectationServiceDTO>();
typeEmploye = new TEmployeDTO();
}
}
public class TEmployeDTO
{
public int id { get; set; }
public string libe { get; set; }
public TEmployeDTO()
{
}
}
public class AffectationServiceDTO
{
public int id { get; set; }
public virtual EmployeDTO employe { get; set; }
public virtual ServiceDTO service { get; set; }
public virtual DroitDTO groupe { get; set; }
public Boolean isPrincipal { get; set; }
public AffectationServiceDTO()
{
service = new ServiceDTO();
groupe = new DroitDTO();
employe = new EmployeDTO();
}
}
public class AdresseDTO
{
public int id { get; set; }
public string numRue { get; set; }
public string nomRue { get; set; }
public string codePostal { get; set; }
public string ville { get; set; }
public string pays { get; set; }
public AdresseDTO()
{
}
}
How I mapp my DTOs with the plain objects
//DomainToViewModel
CreateMap<Adresse, AdresseDTO>().MaxDepth(1);
CreateMap<Personne, PersonneDTO>().MaxDepth(1);
CreateMap<Employe, EmployeDTO>().MaxDepth(1);
CreateMap<AffectationService, AffectationServiceDTO>().MaxDepth(1);
CreateMap<TEmploye, TEmployeDTO>().MaxDepth(1);
//ViewModelToDomain
CreateMap<AdresseDTO, Adresse>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.codePostal, map => map.MapFrom(vm => vm.codePostal))
.ForMember(g => g.nomRue, map => map.MapFrom(vm => vm.nomRue))
.ForMember(g => g.numRue, map => map.MapFrom(vm => vm.numRue))
.ForMember(g => g.pays, map => map.MapFrom(vm => vm.pays))
.ForMember(g => g.ville, map => map.MapFrom(vm => vm.ville));
CreateMap<UtilisateurDTO, Utilisateur>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.dConnexion, map => map.MapFrom(vm => vm.dConnexion))
.ForMember(g => g.dCreation, map => map.MapFrom(vm => vm.dCreation))
.ForMember(g => g.isActive, map => map.MapFrom(vm => vm.isActive))
.ForMember(g => g.isDeleted, map => map.MapFrom(vm => vm.isDeleted))
.ForMember(g => g.login, map => map.MapFrom(vm => vm.login))
.ForMember(g => g.password, map => map.MapFrom(vm => vm.password))
.ForMember(g => g.personne, map => map.MapFrom(vm => vm.personne)).MaxDepth(1);
CreateMap<EmployeDTO, Employe>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.typeEmploye, map => map.MapFrom(vm => vm.typeEmploye))
.ForMember(g => g.affectationServices, map => map.MapFrom(vm => vm.affectationServices))
.ForMember(g => g.utilisateur, map => map.MapFrom(vm => vm.utilisateur)).MaxDepth(1)
.ForMember(g => g.adresse, map => map.MapFrom(vm => vm.adresse)).MaxDepth(1);
CreateMap<TEmployeDTO, TEmploye>().MaxDepth(1)
.ForMember(g => g.libe, map => map.MapFrom(vm => vm.libe));
CreateMap<AffectationServiceDTO, AffectationService>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.employe, map => map.MapFrom(vm => vm.employe)).MaxDepth(1)
.ForMember(g => g.groupe, map => map.MapFrom(vm => vm.groupe)).MaxDepth(1)
.ForMember(g => g.isPrincipal, map => map.MapFrom(vm => vm.isPrincipal))
.ForMember(g => g.service, map => map.MapFrom(vm => vm.service)).MaxDepth(1);
Mapping with EF6 fluent
public PersonneConfiguration()
{
ToTable("Personne");
HasKey<int>(a => a.id);
HasRequired<Adresse>(x => x.adresse);
HasOptional<Utilisateur>(x => x.utilisateur);
Property(a => a.civ).HasColumnType("varchar").HasMaxLength(3);
Property(a => a.nom).HasColumnType("varchar").HasMaxLength(80);
Property(a => a.prenom).HasColumnType("varchar").HasMaxLength(80);
Property(a => a.email).HasColumnType("varchar").HasMaxLength(150);
Property(a => a.tel1).HasColumnType("varchar").HasMaxLength(10);
Property(a => a.tel2).HasColumnType("varchar").HasMaxLength(10);
Property<bool>(a => a.isClient);
Property<bool>(a => a.isFournisseur);
Property<bool>(a => a.isDeleted);
}
public EmployeConfiguration()
{
ToTable("Employe");
HasKey<int>(a => a.id);
HasOptional<TEmploye>(x => x.typeEmploye);
Property<bool>(a => a.isDeleted);
}
public AdresseConfiguration()
{
ToTable("Adresse");
HasKey<int>(a => a.id);
Property(a => a.codePostal).HasColumnType("varchar").HasMaxLength(5);
Property(a => a.nomRue).HasColumnType("varchar").HasMaxLength(150);
Property(a => a.numRue).HasColumnType("varchar").HasMaxLength(10);
Property(a => a.ville).HasColumnType("varchar").HasMaxLength(80);
Property(a => a.pays).HasColumnType("varchar").HasMaxLength(80);
}
public AffectationServiceConfiguration()
{
ToTable("AffectationService");
HasKey<int>(a => a.id);
HasRequired<Employe>(x => x.employe).WithMany(x => x.affectationServices);
HasRequired<Service>(x => x.service);
HasRequired<Droit>(x => x.groupe);
Property<bool>(a => a.isPrincipal);
}
public TEmployeConfiguration()
{
ToTable("TEmploye");
HasKey<int>(a => a.id);
Property(a => a.libe).HasColumnType("varchar").HasMaxLength(100);
}
And the ViewModel taht causes the error
public class EditEmployeViewModel
{
public EmployeDTO personne { get; set; }
public EditEmployeViewModel()
{
personne = new EmployeDTO();
}
}
The stackoverflow occurs when a create a new EmployeDTO(). I tried to solve this by adding .MaxDepth(1) where it's possible, but it doesn't work...
Sorry guys, I know that is a long post, but I don't have any idea about my problem so I try to show you the most relevent part of my code.
Thank you for all, I'm brand new in EF6 so if you have any tips or good pratices to share it will be a pleasure.
thank you for your help.
I didnt' find a real solution so I removed the refererence Personne in my object Adresse to avoid the stackoverflow exception...

EF 4.1 Fluent API dB first relationship mapping problem

I have the following tables,
Product(pro_iIDX[PK], pro_sName)
Manufacturer(man_iIDX[PK], man_sName)
ProductManufacturer(pma_iIDX[PK], pma_iProductRef[FK], pma_iManufacturerRef[FK], pma_bAvailable)
I have the following POCOs,
public class ProductInfo
{
public int IDX { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers
{ get; set; }
}
public class ManufacturerInfo
{
public int IDX { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers
{ get; set; }
}
public class ProductManufacturerInfo
{
public int IDX { get; set; }
public bool Available { get; set; }
public virtual ManufacturerInfo C0Manufacturer { get; set; }
public virtual ProductInfo C0ProductInfo { get; set; }
}
I have used the following mappings without success,
public ProductManufacturerConfiguration()
{
ToTable("ProductManufacturer");
HasKey(p => p.IDX);
Property(p => p.IDX).HasColumnName("pma_iIDX");
Property(p => p.Available).HasColumnName("pma_bAvailable");
Property(p => p.ProductRef).HasColumnName("pma_iProductRef");
Property(p => p.ManufacturerRef).HasColumnName("pma_iManufacturerRef");
//I have tried
HasRequired(p => p.ManufacturerInfo)
.WithMany(c => c.C0ProductManufacturers)
.Map(m => m.MapKey("pma_iManufacturerRef"));
HasRequired(p => p.ProductInfo)
.WithMany(c => c.C0ProductManufacturers)
.Map(m => m.MapKey("pma_iProductRef"));
//As well as
HasRequired(p => p.C0Manufacturer)
.WithMany(c => c.C0ProductManufacturers)
.HasForeignKey(p => p.ManufacturerRef);
HasRequired(p => p.C0Product)
.WithMany(c => c.C0ProductManufacturers)
.HasForeignKey(p => p.C0Product);
}
From my trials, dB first complains about not finding ManufacturerInfo_IDX when I execute the following,
var query = from p in _context.Product
select p;
If I go the code first route, the following table is created,
ProductManufacturer(
pma_iIDX[PK],
pma_iProductRef,
pma_iManufacturerRef,
pma_bAvailable,
ManufacturerInfo_IDX,
ProductInfo_IDX)
Any assistance will be highly appreciated.
I hardly believe that sample you provided is your real code because it even doesn't compile. Is it so hard to copy a real code to show a problem?
This works:
public class ProductInfo
{
public int IDX { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers
{ get; set; }
}
public class ManufacturerInfo
{
public int IDX { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers
{ get; set; }
}
public class ProductManufacturerInfo
{
public int IDX { get; set; }
public bool Available { get; set; }
public int ManufacturerRef { get; set; }
public virtual ManufacturerInfo C0Manufacturer { get; set; }
public int ProductRef { get; set; }
public virtual ProductInfo C0ProductInfo { get; set; }
}
public class ProductManufacturerConfiguration : EntityTypeConfiguration<ProductManufacturerInfo>
{
public ProductManufacturerConfiguration()
{
ToTable("ProductManufacturer");
HasKey(p => p.IDX);
Property(p => p.IDX).HasColumnName("pma_iIDX");
Property(p => p.Available).HasColumnName("pma_bAvailable");
Property(p => p.ProductRef).HasColumnName("pma_iProductRef");
Property(p => p.ManufacturerRef).HasColumnName("pma_iManufacturerRef");
//I have tried
HasRequired(p => p.C0Manufacturer)
.WithMany(c => c.C0ProductManufacturers)
.Map(m => m.MapKey("pma_iManufacturerRef"));
HasRequired(p => p.C0ProductInfo)
.WithMany(c => c.C0ProductManufacturers)
.Map(m => m.MapKey("pma_iProductRef"));
//As well as
HasRequired(p => p.C0Manufacturer)
.WithMany(c => c.C0ProductManufacturers)
.HasForeignKey(p => p.ManufacturerRef);
HasRequired(p => p.C0ProductInfo)
.WithMany(c => c.C0ProductManufacturers)
.HasForeignKey(p => p.ProductRef);
}
}
The main problem is that your key for ProductManufacturerInfo should not really be IDX. IDX is more of a "payload" in your many-to-many association. One way to fix this is to specify a true key and then the mapping is easy:
public class ProductManufacturerInfo
{
public int IDX { get; set; }
public bool Available { get; set; }
public int C0ManufacturerIDX { get; set; }
public virtual ManufacturerInfo C0Manufacturer { get; set; }
public int C0ProductInfoIDX { get; set; }
public virtual ProductInfo C0ProductInfo { get; set; }
}
Then your mapping:
public class ProductManufacturerConfiguration
: EntityTypeConfiguration<ProductManufacturerInfo>
{
public ProductManufacturerConfiguration()
{
ToTable("ProductManufacturer");
HasKey(p => new { p.C0ManufacturerIDX, p.C0ProductInfoIDX });
Property(p => p.IDX)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}

Categories