I find many topics but i cant do hasMany in my tests, i have:
public class ProductModel
{
public virtual Guid Id { get; set; }
public virtual string ProductName { get; set; }
public virtual IList<LicenseModel> License { get; set; }
public ProductModel()
{
License = new List<LicenseModel>();
}
}
public class LicenseModel
{
public virtual Guid Id { get; set; }
public virtual double Price { get; set; }
public virtual string Period { get; set; }
public virtual int Discount { get; set; }
public virtual ProductModel ProductModel { get; set; }
}
And some try of mapping:
public class ProductMap: ClassMap<ProductModel>
{
public ProductMap()
{
Id(x => x.Id);
Map(x => x.ProductName);
HasMany<LicenseModel>(x => x.License)
.KeyColumn("Id");
Table("Product");
}
}
public class LicenseMap: ClassMap<LicenseModel>
{
public LicenseMap()
{
Id(x => x.Id);
Map(x => x.Period);
Map(x => x.Price);
Map(x => x.Discount);
References(x => x.ProductModel)
.Class<ProductModel>()
.Columns("LicenseId");
Table("License");
}
}
in that way my base look like this:
Table Product don't look cool :(
Some ideas?
Thank in Advice.
I think these mappings will help you
public class ProductMap : ClassMap<ProductModel>
{
public ProductMap()
{
Table("Product");
Id(x => x.Id);
Map(x => x.ProductName);
HasMany<LicenseModel>(x => x.License)
.Inverse()
.KeyColumn("ProductModelId");
}
}
public class LicenseMap : ClassMap<LicenseModel>
{
public LicenseMap()
{
Table("License");
Id(x => x.Id);
Map(x => x.Period);
Map(x => x.Price);
Map(x => x.Discount);
References<ProductModel>(x => x.ProductModel)
.Column("ProductModelId");
}
}
Related
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 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 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
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
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);
}
}