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
Related
I'm struggling to map 2 objects. Basically have Product which is my EF model, and I'm mapping this to ProductDto, which has FileDto.
I'd like to map Product.FileName to ProductDto.File.Internal name, how to do this?
Classes below.
public class Product : BaseEntity<long>
{
[MaxLength(100)]
public string Name { get; set; }
[MaxLength(100)]
public string Barcode { get; set; }
public int ShelfLife { get; set; }
public int Weight { get; set; }
public bool HasAllergens { get; set; }
[MaxLength(100)]
public string FileName { get; set; }
[ForeignKey("Id")]
public int CustomerId { get; set; }
public virtual ICollection<ProductIngredient> ProductIngredient { get; set; }
public virtual ICollection<Nutrition> Nutritions { get; set; }
public virtual ICollection<ProductComposition> Composition { get; set; }
public virtual IList<ProductionProcess> ProductionProcess { get; set; }
}
public class ProductDto
{
public long Id { get; set; }
public DateTime CretedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public string Name { get; set; }
public string Barcode { get; set; }
public int ShelfLife { get; set; }
public int Weight { get; set; }
public bool HasAllergens { get; set; }
public int CustomerId { get; set; }
public FileDto File { get; set; }
public IList<IngredientDto> Ingredients { get; set; }
public IList<NutritionDto> Nutritions { get; set; }
public IList<ProductCompositionDto> Composition { get; set; }
public IList<ProductionProcessDto> ProductionProcess { get; set; }
}
public class ProductionProcessDto
{
public string Key { get; set; }
public string Value { get; set; }
public FileDto File { get; set; }
}
public class NutritionDto
{
public string Key { get; set; }
public string Value { get; set; }
}
public class ProductCompositionDto
{
public string Key { get; set; }
public string Value { get; set; }
}
File Dto:
public class FileDto
{
public string Base64EncodedFile { get; set; }
public string OriginalName { get; set; }
public string InternalName { get; set; }
public string Type { get; set; }
}
Automapper so far:
//Product
CreateMap<Nutrition, NutritionDto>().ReverseMap();
CreateMap<ProductComposition, ProductCompositionDto>().ReverseMap();
CreateMap<ProductionProcessDto, ProductionProcess>()
.ForMember(dest => dest.FileInternalName, opt => opt.MapFrom(src => src.File.InternalName))
.ForMember(dest => dest.FileOriginalName, opt => opt.MapFrom(src => src.File.OriginalName))
.ReverseMap();
CreateMap<Product, ProductDto>()
.ForMember(d => d.File, o => o.MapFrom(s => Mapper.Map<Product, FileDto>(s)))
.ForMember(d => d.Nutritions, o => o.MapFrom(s => s.Nutritions))
.ForMember(d => d.Composition, o => o.MapFrom(s => s.Composition))
.ForMember(d => d.ProductionProcess, o => o.MapFrom(s => s.ProductionProcess))
.ForMember(d => d.Ingredients, o => o.MapFrom(s => s.ProductIngredient.Select(pi => pi.Ingredients)))
.ReverseMap();
CreateMap<ProductDto, Product>()
.ForMember(d => d.FileName, o => o.MapFrom(s => s.File.InternalName))
.ReverseMap();
I am able to map from ProductDto (on data post) to Product but not other way around, all help much appreciated
Thanks
This code solved my issue:
.ForMember(d => d.File, o => o.MapFrom(model => new FileDto { InternalName = model.FileName }))
Applied to:
CreateMap<Product, ProductDto>()
I have a class called DadosvalmetSam (Parent) which have a one-to-many relationship with DadosfuncionarioSam(Child).
Classes and mapping below:
DadosvalmetSam
public class DadosvalmetSam
{
public DadosvalmetSam()
{
dadosFuncionarios = new List<DadosfuncionarioSam>();
}
public virtual int codigoDvm { get; set; }
public virtual MetanomeSam codmenDvm { get; set; }
public virtual ValidadeSam codvalDvm { get; set; }
public virtual IList<DadosfuncionarioSam> dadosFuncionarios { get; set; }
}
public class DadosvalmetSamMap : ClassMap<DadosvalmetSam>
{
public DadosvalmetSamMap()
{
Table("dadosvalmet_sam");
Id(x => x.codigoDvm).Column("codigo_dvm").GeneratedBy.Identity().Not.Nullable();
References(x => x.codmenDvm).Column("codmen_dvm").Not.Nullable().Not.LazyLoad();
References(x => x.codvalDvm).Column("codval_dvm").Not.Nullable().Not.LazyLoad();
HasMany(x => x.dadosFuncionarios).KeyColumn("coddvm_dfu").Cascade.All().Not.LazyLoad();
}
}
DadosfuncionarioSam
public class DadosfuncionarioSam
{
public DadosfuncionarioSam()
{
}
public virtual DadosvalmetSam coddvmDfu { get; set; }
public virtual Funcionario codfunDfu { get; set; }
public virtual int codigoDfu { get; set; }
public virtual float? valorDfu { get; set; }
}
public class DadosfuncionarioSamMap : ClassMap<DadosfuncionarioSam>
{
public DadosfuncionarioSamMap()
{
Table("dadosfuncionario_sam");
Id(x => x.codigoDfu).Column("codigo_dfu").GeneratedBy.Identity().Not.Nullable();
References(x => x.codfunDfu).Column("codfun_dfu").Not.Nullable().Not.LazyLoad();
Map(x => x.valorDfu).Column("valor_dfu").Not.Nullable();
References(x => x.coddvmDfu).Column("coddvm_dfu").Nullable();
}
}
When I'm inserting a DadosvalmetSam, NH inserts all the data inside the IList, that is exactly what I expected. But when I'm Updating a DadosvalmetSam and on this update I removed a row from IList, NH just set the FK in the DadosfuncionarioSam to null and it is not deleting the record, How could I solve this problem?
Have you tried
HasMany(x => x.dadosFuncionarios).KeyColumn("coddvm_dfu").Cascade.DeleteOrphans().Not.LazyLoad();
I'm looking for any how to or documentation about use of Embeddable with FluentNHibernate exactly with in the Hibernate. Does has any way to works with Embeddable in FluentNHibernate, if not has what's the best way to simulate this ?
Finally I found some solution. It's very simple like Embeddable of JPA/Hibernate.
Found here: https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping#components
Then I did.
public class Cliente {
public virtual long id { set; get; }
public virtual long codigo { set; get; }
public virtual String nome { set; get; }
public virtual String sexo { set; get; }
public virtual String cpf { set; get; }
public virtual String rg { set; get; }
public virtual DateTime dtNascimento { set; get; }
public virtual Endereco endereco { set; get; } //Embeddable
public Cliente() { }
}
public class Endereco {
public String endereco;
public String numero;
public String bairro;
public String complemento;
public String cidade;
public String cep;
public EstadosBrasil uf;
public Endereco() {
}
}
Mapping
public class ClienteMap : ClassMap<Cliente> {
public ClienteMap() {
Table("CLIENTE");
Id(c => c.id).GeneratedBy.Native();
Map(c => c.codigo);
Map(c => c.nome);
Map(c => c.sexo).Length(2);
Map(c => c.cpf);
Map(c => c.rg);
Map(c => c.dtNascimento).CustomType<DateTime>();
//embeddable
Component(c => c.endereco, e => {
e.Map(c => c.endereco);
e.Map(c => c.numero).CustomType<int>();
e.Map(c => c.bairro);
e.Map(c => c.complemento);
e.Map(c => c.cidade);
e.Map(c => c.cep);
e.Map(c => c.uf).CustomType<GenericEnumMapper<EstadosBrasil>>();
});
}
}
I'm using FluentNhibernate for my C# application i would like to know how to join three tables which not having Foreign keys defined. Lets assume i have following table structure,
Student [StudentID, Name1, Name2, ClassID ]
Class [ClassID, Name, SchoolID]
School [SchoolID, SchoolName]
I need to join above three tables like this
SELECT a.Name1,a.Name2,b.Name,c.SchoolName FROM Student a, Class b, School c WHERE a.ClassID = b.ClassID AND b.SchoolID = c.SchoolID
I have following class structure for ORM
public class Student
{
public virtual int StudentID { get; set; }
public virtual string Name1 { get; set; }
public virtual string Name2 { get; set; }
public virtual int ClassID { get; set; }
}
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Id(x => x.StudentID ).Column("student_id");
Map(x => x.Name1 ).Column("name_1");
Map(x => x.Name1 ).Column("name_2");
Map(x => x.ClassID).Column("class_Id");
Table("student");
}
}
public class Classt
{
public virtual int ClassID { get; set; }
public virtual string Name { get; set; }
public virtual int SchoolID { get; set; }
}
public class ClassMap : ClassMap<Class>
{
public ClassMap ()
{
Id(x => x.ClassID ).Column("class_id");
Map(x => x.Name ).Column("name");
Map(x => x.SchoolID).Column("school_Id");
Table("class");
}
}
public class School
{
public virtual int SchooID { get; set; }
public virtual string Name { get; set; }
}
public class SchoolMap : ClassMap<School>
{
public SchoolMap ()
{
Id(x => x.ClassID ).Column("class_id");
Map(x => x.Name ).Column("name");
Table("school");
}
}
You should go something like this;
public class Student
{
public virtual int StudentID { get; set; }
public virtual string Name1 { get; set; }
public virtual string Name2 { get; set; }
public virtual List<Class> ClassList { get; set; }
}
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Id(x => x.StudentID ).Column("student_id");
Map(x => x.Name1 ).Column("name_1");
Map(x => x.Name1 ).Column("name_2");
HasMany(x => x.Class).KeyColumn("ClassId")
Table("student");
}
}
public class Class
{
public virtual int ClassID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Student> StudentList { get; set; }
public virtual School School { get; set; }
}
public class ClassMap : ClassMap<Class>
{
public ClassMap ()
{
Id(x => x.ClassID ).Column("class_id");
Map(x => x.Name ).Column("name");
HasMany(x => x.Student).KeyColumn("StudentId")
References(x => x.School)
Table("class");
}
}
public class School
{
public virtual int SchoolID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Class> ClassList { get; set; }
}
public class SchoolMap : ClassMap<School>
{
public SchoolMap ()
{
Id(x => x.SchoolID ).Column("SchoolId");
Map(x => x.Name ).Column("name");
HasMany(x => x.ClassList).KeyColumn("ClassId")
Table("school");
}
}
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);