EF 4.1 Fluent API dB first relationship mapping problem - c#

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

Related

Entity Framework Core Multiple sub objects Include / ThenInclude

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

C# - Entity Framework Code first, lazy loading not working

I have some issues with Entity Framework 6 code-first with a SQL Server database.
I have an existing database, and I want to use them with Entity Framework. With Visual Studio 2017, I add a new item of type ADO.NET Entity Data Model to generate the code-first classes for the first time.
Once the classes are generated, I start to use the objects to interact with the database. Here I have some issues with lazy loading. Some navigation properties are null. The navigation properties are marked with virtual and lazy loading and ProxyCreationEnabled are true.
These screenshots illustrate the problem:
I can share the classes generated by Visual Studio:
public partial class ETRFContext : DbContext
{
static string conn = "My connection string";
public ETRFContext() : base(conn)
{
}
public virtual DbSet<Acessos> Acessos { get; set; }
public virtual DbSet<Caracteristicas> Caracteristicas { get; set; }
public virtual DbSet<CircuitoAprovacoes> CircuitoAprovacoes { get; set; }
public virtual DbSet<EstadosEstudo> EstadosEstudo { get; set; }
public virtual DbSet<Estudos> Estudos { get; set; }
public virtual DbSet<GruposAcesso> GruposAcesso { get; set; }
public virtual DbSet<HistoricoDetalhadoEstudo> HistoricoDetalhadoEstudo { get; set; }
public virtual DbSet<MembrosGruposAcesso> MembrosGruposAcesso { get; set; }
public virtual DbSet<Normas> Normas { get; set; }
public virtual DbSet<Paises> Paises { get; set; }
public virtual DbSet<PrecedenciasEstudos> PrecedenciasEstudos { get; set; }
public virtual DbSet<PrecedenciasProgramas> PrecedenciasProgramas { get; set; }
public virtual DbSet<Programas> Programas { get; set; }
public virtual DbSet<Projetos> Projetos { get; set; }
public virtual DbSet<RegistosCAD> RegistosCAD { get; set; }
public virtual DbSet<TiposEstadoEstudo> TiposEstadoEstudo { get; set; }
public virtual DbSet<TiposMensagem> TiposMensagem { get; set; }
public virtual DbSet<TiposProjeto> TiposProjeto { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Caracteristicas>()
.Property(e => e.Chave)
.IsFixedLength();
modelBuilder.Entity<Caracteristicas>()
.Property(e => e.Valor)
.IsFixedLength();
modelBuilder.Entity<Estudos>()
.HasMany(e => e.EstadosEstudo)
.WithRequired(e => e.Estudos)
.HasForeignKey(e => e.idEstudo);
modelBuilder.Entity<Estudos>()
.HasMany(e => e.HistoricoDetalhadoEstudo)
.WithRequired(e => e.Estudos)
.HasForeignKey(e => e.idEstudo);
modelBuilder.Entity<Estudos>()
.HasMany(e => e.PrecedenciasEstudos)
.WithRequired(e => e.Estudos)
.HasForeignKey(e => e.idEstudo);
modelBuilder.Entity<Estudos>()
.HasMany(e => e.PrecedenciasEstudos1)
.WithRequired(e => e.Estudos1)
.HasForeignKey(e => e.idEstudoPrecedencia)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Estudos>()
.HasMany(e => e.RegistosCAD)
.WithRequired(e => e.Estudos)
.HasForeignKey(e => e.idEstudo);
modelBuilder.Entity<GruposAcesso>()
.HasMany(e => e.Acessos)
.WithRequired(e => e.GruposAcesso)
.HasForeignKey(e => e.idGruposAcesso);
modelBuilder.Entity<GruposAcesso>()
.HasMany(e => e.MembrosGruposAcesso)
.WithRequired(e => e.GruposAcesso)
.HasForeignKey(e => e.idGrupoAcesso);
modelBuilder.Entity<Paises>()
.HasMany(e => e.Projetos)
.WithOptional(e => e.Paises)
.HasForeignKey(e => e.id_Pais);
modelBuilder.Entity<Programas>()
.HasMany(e => e.Acessos)
.WithRequired(e => e.Programas)
.HasForeignKey(e => e.idPrograma);
modelBuilder.Entity<Programas>()
.HasMany(e => e.CircuitoAprovacoes)
.WithRequired(e => e.Programas)
.HasForeignKey(e => e.idPrograma);
modelBuilder.Entity<Programas>()
.HasMany(e => e.Estudos)
.WithRequired(e => e.Programas)
.HasForeignKey(e => e.idProgramas);
modelBuilder.Entity<Programas>()
.HasMany(e => e.PrecedenciasProgramas)
.WithRequired(e => e.Programas)
.HasForeignKey(e => e.idProgramaPrecedencia);
modelBuilder.Entity<Programas>()
.HasMany(e => e.PrecedenciasProgramas1)
.WithRequired(e => e.Programas1)
.HasForeignKey(e => e.idPrograma)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Projetos>()
.HasMany(e => e.Caracteristicas)
.WithRequired(e => e.Projetos)
.HasForeignKey(e => e.id_projeto);
modelBuilder.Entity<Projetos>()
.HasMany(e => e.Estudos)
.WithRequired(e => e.Projetos)
.HasForeignKey(e => e.idProjetos);
modelBuilder.Entity<TiposEstadoEstudo>()
.HasMany(e => e.EstadosEstudo)
.WithRequired(e => e.TiposEstadoEstudo)
.HasForeignKey(e => e.idEstado);
modelBuilder.Entity<TiposMensagem>()
.HasMany(e => e.HistoricoDetalhadoEstudo)
.WithRequired(e => e.TiposMensagem)
.HasForeignKey(e => e.idTipoMensagem);
modelBuilder.Entity<TiposProjeto>()
.HasMany(e => e.Programas)
.WithRequired(e => e.TiposProjeto)
.HasForeignKey(e => e.idTiposProjeto);
modelBuilder.Entity<TiposProjeto>()
.HasMany(e => e.Projetos)
.WithRequired(e => e.TiposProjeto)
.HasForeignKey(e => e.id_Tipo)
.WillCascadeOnDelete(false);
}
}
Now this is the class Estudos:
public partial class Estudos
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Estudos()
{
EstadosEstudo = new HashSet<EstadosEstudo>();
HistoricoDetalhadoEstudo = new HashSet<HistoricoDetalhadoEstudo>();
PrecedenciasEstudos = new HashSet<PrecedenciasEstudos>();
PrecedenciasEstudos1 = new HashSet<PrecedenciasEstudos>();
RegistosCAD = new HashSet<RegistosCAD>();
}
public int id { get; set; }
public int idProjetos { get; set; }
public int idProgramas { get; set; }
[Required]
[StringLength(25)]
public string NomeEstudo { get; set; }
[Required]
[StringLength(5)]
public string Utilizador { get; set; }
public DateTime DataInicial { get; set; }
public DateTime? DataFinal { get; set; }
public bool Producao { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<EstadosEstudo> EstadosEstudo { get; set; }
public virtual Programas Programas { get; set; }
public virtual Projetos Projetos { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<HistoricoDetalhadoEstudo> HistoricoDetalhadoEstudo { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PrecedenciasEstudos> PrecedenciasEstudos { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PrecedenciasEstudos> PrecedenciasEstudos1 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<RegistosCAD> RegistosCAD { get; set; }
}
And the class Projetos:
public partial class Projetos
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Projetos()
{
Caracteristicas = new HashSet<Caracteristicas>();
Estudos = new HashSet<Estudos>();
}
public int id { get; set; }
[Required]
[StringLength(15)]
public string projeto { get; set; }
public int id_Tipo { get; set; }
[StringLength(50)]
public string Cliente { get; set; }
[StringLength(50)]
public string Designacao { get; set; }
public int? id_Pais { get; set; }
public int? CalculoEletrico { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Caracteristicas> Caracteristicas { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Estudos> Estudos { get; set; }
public virtual Paises Paises { get; set; }
public virtual TiposProjeto TiposProjeto { get; set; }
}
One thing that I noticed, is when I query the Database for a "Estudo" I don't get the navigation property "Projetos", but when I ask the database for a "Projeto" I get the navigation property "Estudos".
Another thing is that when I query the database for a "Estudo" I don't get the navigation property "Projetos", but if then I query the database for a "Projeto" where id = Estudo.idProjetos, the navigation property "Projetos" in "Estudos" that was null before, automatically fills up with the value of associated "Projeto".
So, anyone have some tips to solve this issue?
Appreciate your help.
In your Estudos entity, can you add ForeignKey attribute to Programas and Projetos properties.
[ForeignKey("idProgramas")]
public virtual Programas Programas { get; set; }
[ForeignKey("idProjetos")]
public virtual Projetos Projetos { get; set; }
And when querying Estudos include Programas and Projetos.
db.Estudos
.Include(x => x.Programas)
.Include(x => x.Projetos)
.ToList();

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...

Entity Framework - The foreign key component … is not a declared property on type

I have the following Model
public class FilanthropyEvent : EntityBase, IDeleteable
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime EventDate { get; set; }
public string Description { get; set; }
public decimal Target { get; set; }
public decimal EntryFee { get; set; }
public bool Deleted { get; set; }
public ICollection<EventAttendee> EventAttendees { get; set; }
}
public class Attendee : EntityBase, IDeleteable
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool MailingList { get; set; }
public bool Deleted { get; set; }
public ICollection<EventAttendee> EventAttendees { get; set; }
}
Events and Attendees is a many to many relationship but I needed another property on the association so I created an association entity
public class EventAttendee : EntityBase
{
public int FilanthropyEventId { get; set; }
public int AttendeeId { get; set; }
public bool InActive { get; set; }
public virtual Attendee Attendee { get; set; }
public virtual FilanthropyEvent FilanthropyEvent { get; set; }
}
These are the configurations for each FilanthropyEvent and Attendee
public class FilanthropyEventConfiguration : EntityTypeConfiguration<FilanthropyEvent>
{
public FilanthropyEventConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.EventAttendees).WithRequired(x => x.FilanthropyEvent).HasForeignKey(x => x.FilanthropyEvent);
}
}
public AttendeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.EventAttendees).WithRequired(x => x.Attendee).HasForeignKey(x => x.AttendeeId);
}
public class EventAttendeesConfiguration : EntityTypeConfiguration<EventAttendee>
{
public EventAttendeesConfiguration()
{
HasKey(x => new {x.FilanthropyEventId, x.AttendeeId});
}
}
When I try and initialise the database via the update-database command in the package manager console I get the following error.
System.InvalidOperationException: The foreign key component 'FilanthropyEvent' is not a declared property on type 'EventAttendee'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
I realise I'm probably missing a mapping in the EventAttendeesConfiguration class but what would be the correct mapping to model this relationship?
This code
HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEvent);
Should be
HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEventId);

FNH Many-to-Many relationship with attributes

I have three tables as follow:
Content:
public class Content
{
public Content()
{
ContentFieldItems = new List<ContentFieldItem>();
ContentFields = new Dictionary<ContentField, ContentFieldItem>();
People = new List<Person>();
}
public virtual int id { get; set; }
public virtual int categoryid { get; set; }
public virtual int userid { get; set; }
public virtual DateTime? date { get; set; }
public virtual int status { get; set; }
public virtual IDictionary<ContentField, ContentFieldItem> ContentFields { get; set; }
public virtual IList<ContentFieldItem> ContentFieldItems { get; set; }
public virtual IList<Person> People { get; set; }
public virtual string title { get; set; }
}
public class ContentMap : ClassMap<Content>
{
Id(x => x.id);
Map(x => x.categoryid);
Map(x => x.userid);
Map(x => x.date);
Map(x => x.status);
Map(x => x.title);
HasMany<ContentFieldItem>(x=>x.ContentFields)
.Table("ContentFieldItem")
.KeyColumn("contentid")
.AsMap(f => f.ContentField)
.Component(c =>
{
c.ParentReference(m => m.Content);
c.References(m => m.ContentField);
c.Map(m => m.fieldadditionalinfo);
c.Map(m => m.fieldvalue);
})
.Inverse()
.Cascade.AllDeleteOrphan();
...
}
ContentField:
public class ContentField
{
public ContentField()
{
Contents = new List<Content>();
}
public virtual int id { get; set; }
public virtual string field { get; set; }
public virtual IList<Content> Contents { get; set; }
public virtual void AddContentField(Content content,
ContentFieldItem contentfielditem)
{
Contents.Add(content);
content.ContentFields.Add(this, contentfielditem);
}
}
public class ContentFieldMap : ClassMap<ContentField>
{
public ContentFieldMap()
{
Id(x => x.id);
Map(x => x.field);
Map(x => x.categoryid);
Map(x => x.status);
Map(x => x.type);
Map(x => x.showonall);
}
}
ContentFieldItem:
public class ContentFieldItem
{
public virtual int id { get; set; }
public virtual string fieldvalue { get; set; }
public virtual string fieldadditionalinfo { get; set; }
public virtual Content Content { get; set; }
public virtual ContentField ContentField { get; set; }
}
public class ContentFieldItemMap : ClassMap<ContentFieldItem>
{
public ContentFieldItemMap()
{
Id(x => x.id);
Map(x => x.fieldvalue);
Map(x => x.fieldadditionalinfo);
References(x => x.Content, "contentid");
References(x => x.ContentField, "fieldid");
}
}
These three tables at the same time I want to create a way of inter-related.
I want to create a ContentFieldItem with relation Content through contentid and ContentField through fieldid and some more parameters.I think this can be done with many-to-many without parameters but i have to do with parameters, how is this possible with fluent nhibernate?
and now m getting "Could not determine type for: Project.Models.ContentField".
i would do
public class Content
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual IDictionary<ContentField, ContentFieldInfo> Fields { get; set; }
}
public class ContentField
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class ContentFieldInfo
{
public virtual Content Content { get; set; }
public virtual ContentField Field { get; set; }
public virtual string Values { get; set; }
public virtual string AdditionalInfo { get; set; }
}
class ContentMap : ClassMap<Content>
{
public ContentMap()
{
Id(...);
Map(c => c.Title);
HasMany(c => c.Fields)
.Table("ContentFieldItem")
.KeyColumn("contentid")
.AsMap(fieldinfo => fieldinfo.Field)
.Component(comp =>
{
comp.ParentReference(fi => fi.Content);
comp.References(fi => fi.ContentField);
comp.Map(fi => fi.Value);
...
});
}
}
class ContentFieldMap : ClassMap<ContentField>
{
public ContentFieldMap()
{
Id(...);
Map(c => c.Name);
}
}
can you clarify what you mean with ContentField through fieldid and some more parameters.?
Hope this helps

Categories