1

I am trying to migrate to Fluent NHibernate and have the following problem :(

I have a a number of classes called in the manner of CompanyXXXXXX, which all have a PrimaryKey of "CompanyId" which is of type Company.

A HBM Mapping file which i was using until name was this:

  <class name="CompanyAccounting" table="Company_Accounting" >
    <id column="CompanyID" type="Int32">
      <generator class="foreign">
        <param name="property">Company</param>
      </generator>
    </id>
    <one-to-one name="Company" constrained="true" />
  </class>

The entity is as follows:

public class CompanyAccounting
{
    public virtual Company Company {get;set;}        
}

Is it possible to use some sort of AutoMapping feature, as i have a dozen of these classes and there is likely to be more.

I have tried the following:

    public class CustomPrimaryKeyConvention : IIdConvention
    {
        public void Apply(IIdentityInstance instance)
        {
            var type = instance.EntityType;
            if (type.Name.StartsWith("Company") && type.Name.Length > 7)
            {
                instance.CustomType(typeof(Company));
                instance.Column("CompanyId");
            } 
            else
            {
                instance.Column(instance.EntityType.Name + "Id");
            }
        }
    }

EDIT : But my If(...)[For "CompanyAccounting" Type] doesn't even get hit. Any suggestions?

Exception:

The entity 'CompanyAccounting' doesn't have an Id mapped. 
Use the Id method to map your identity property. For example: Id(x => x.Id).
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118

1 Answers1

1

Did you register that convention with Fluent Nhibernate?

You should have something along the lines

AutoMap.AssemblyOf<CompanyAccounting>()
       .Conventions.AddFromAssemblyOf<CustomPrimaryKeyConvention>()
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • Hi forgot to mention that it doesnt get hit for CompanyAccounting, it gets hit for previous, classes, but it doesnt call the method Apply for CompanyAccounting, the exception gets hit beforehand. – Michal Ciechan May 19 '11 at 13:05
  • So your convention works for other classes but not CompanyAccounting? Is CompanyAccounting in the same assembly as classes that it does work for? – Chris Marisic May 19 '11 at 14:20
  • yes... i think the problem is because CompanyAccounting has no public Id{get;set;}, but i don't know how i can tell automapper not to look for Id field, but instead use Company field. – Michal Ciechan May 19 '11 at 14:36
  • Yes answers which question(s)? – Chris Marisic May 20 '11 at 14:53
  • @Chris sorry, Yes CompanyAccounting is in the same assembly as the other classes and the same namespace. And the convention works for other classes, but when i step through the debug, it crashes as soon as it tries to do the initialisation for CompanyAccounting – Michal Ciechan May 23 '11 at 13:05
  • I'm not really sure I think you should try the FNH group. – Chris Marisic May 23 '11 at 13:16