NHibernate: One-to-many - c#

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

Related

How to eager fetch multi level objects with NHibernate

I have the following Entity's:
public class Parent
{
public string Id { get; set; }
public Ilist<Child> Children { get; set; }
}
public class Child
{
public string Id { get; set; }
public Ilist<Parent> Parents { get; set; }
public Ilist<Grandchild> GrandChildren { get; set; }
}
public class Grandchild
{
public string Id { get; set; }
public Child parent { get; set; }
}
And their mappings:
public class ParentMap
{
HasManyToMany(x => x.Children).Table("P_C").LazyLoad();
}
public class ChildMap
{
HasManyToMany(x => x.Parent).Table("P_C").Inverse();
HasMany(x => x.Parents).KeyColumn("ParentID").Cascade.All().Inverse();
}
public class GrandchildMap
{
References(x => x.parent).Column("ParentID").Not.Nullable();
}
I want to fetch Eager all the levels using NHibernate 4
When i try Getting a single Parent by ID like:
var Parent = session.QueryOver<Parent>().Where(x => x.Id == Id).
Fetch(x => x.Children).Eager.
Fetch(x => x.Children.First().GrandChildren).Eager.
TransformUsing(Transformers.DistinctRootEntity).
List<Parent>().FirstOrDefault();
I get "cannot simultaneously fetch multiple bags" Exception

How to join three tables using FluentNhibernate

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

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

Best method to search on table with self join

I have classes like these in c#:
public class Family
{
public virtual string Description { get; set; }
public virtual Family Parent { get; set; }
public virtual IList<Family> Childrens { get; set; }
public virtual IList<Produt> Produts { get; set; }
}
public class Produt
{
public virtual string Description { get; set; }
public virtual Family Family { get; set; }
}
After this, I did the mappings:
public partial class FamilyMap : AbstractEntityMap<Family>
{
public FamilyMap()
{
Map(x => x.Description).Length(100);
References(x => x.Parent, "IdParent");
HasMany(x => x.Childrens).KeyColumns.Add("IdParent");
HasMany(x=>x.Produts);
}
}
public partial class ProdutMap : AbstractEntityMap<Produt>
{
public ProdutMap()
{
Map(x => x.Description).Length(100);
References(x => x.Family);
}
}
Imagine a Family with a lot of childrens and each children with a lot of childrens... What the best method to know the produts that belong to parent with id=3, for example...
P.S. Id is in the AbstractEntityMap.
use recursive With statement at SQL side

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