Mappings with legacy Database.

barrett's Avatar

barrett

06 Jul, 2010 06:18 PM via web

My legacy table schema looks as follows:

Create Table private_projection_assumptions
(
private_projection_assumption_id  INT Identity(1,1) Primary Key
,multiple decimal(18,10) NOT NULL
,drawn decimal(18,10) NOT NULL
,notes nvarchar(max)
,Constraint valid_mulitple CHECK ( multiple > 0 AND multiple < 100)
,Constraint valid_draw_amount CHECK( drawn >= 0.0 and drawn <= 1.0)
);


Create Table private_projection_rates
(
 private_projection_rates_id INT Identity(1,1) PRIMARY KEY
,private_projection_assumption_id int FOREIGN KEY REFERENCES    
private_projection_assumptions(private_projection_assumption_id)
,year_id int Not Null
,projected_call_rate decimal(18,10)  
,projected_distribution_rate decimal(18,10)
,Constraint valid_call_rate Check(projected_call_rate >= 0 AND projected_call_rate <= 1.00)
,Constraint valid_distribution_rate Check(projected_distribution_rate >= 0 and projected_distribution_rate <= 1.00)
 )

I have defined my business object as follows

public class PrivateProjectionAssumptions    //corresponds to  private_projection_assumptions
    {
    public virtual int investment_vehicle_portfolio_id { get; set; }
    public virtual int private_projection_assumption_id { get; set; }
    public virtual decimal multiple { get; set; }
    public virtual decimal percent_drawn { get; set; }
    //public virtual IList<PrivateEquityRateAssumptions> RateAssumptions { get; set; }
   }

And

 public class PrivateEquityRateAssumptions
  {
    public virtual int private_projection_rate_id { get; set; }
    public virtual int private_projection_assumption_id { get; set; }
    public virtual int year_id { get; set; }
    public virtual decimal projected_call_rate { get; set; }
    public virtual decimal projected_distribution_rate { get; set; }
    public virtual PrivateProjectionAssumptions FundAssumptions {get;set;}
   }

My Mappings are :

 public PrivateProjectionAssumptionsMap()
   {
       Table("private_projection_assumptions");
       SchemaAction.None();
       Id(x => x.private_projection_assumption_id).Column("private_projection_assumption_id");
       Map(x => x.multiple).Column("multiple");
       Map(x => x.percent_drawn).Column("drawn");
       HasMany(x => x.RateAssumptions).Inverse().Cascade.All();
     }

public PrivateEquityRateAssumptionsMap()
    {
        Table("private_projection_rates");
        SchemaAction.None();
        Id(x => x.private_projection_rate_id).Column("private_projection_rates_id");
        Map(x => x.private_projection_assumption_id).Column("private_projection_assumption_id");
        Map(x => x.year_id).Column("year_id");
        Map(x => x.projected_call_rate).Column("projected_call_rate");
        Map(x => x.projected_distribution_rate).Column("projected_call_rate");
        References(x => x.FundAssumptions);
     }

My fluent configuration is

  private ISessionFactory CreateSessionFactory()
    {

        return Fluently.Configure()
             .Database(MsSqlConfiguration.MsSql2008
                           .ShowSql()
                           .ConnectionString(
                           C => C.Is("Data Source=GREED\\ANALYTICS;Initial Catalog=CashFlowMock;Integrated Security=SSPI;")))
             .Mappings(M => M.FluentMappings.AddFromAssemblyOf<PrivateEquityRateAssumptions>())
             .ExposeConfiguration(cfg =>
             {
                 new SchemaExport(cfg)
                 .Create(false, false);
             })
             .BuildSessionFactory();

    }

My db tables are populated and I am trying the following to establish that my mappings are correct

      FluentNHibContext nhibcontext = new FluentNHibContext();
        PrivateProjectionAssumptionsRepository FundAssumptionRepo = new PrivateProjectionAssumptionsRepository(nhibcontext);
        IList<PrivateProjectionAssumptions> list =  FundAssumptionRepo.List();

The list has a count of zero e,g, no objects are being returned.
As you can see I have enable ShowSql and nothing is being returned in the output window depicting any sql.
Also I can rename the tables in the mapping files to something nonsense and no error is returned
Example Table("private_projection_rates"); -> Table("garbage");

What do you guys thing the issue may be?

  1. Support Staff 2 Posted by Paul Batum on 07 Jul, 2010 11:33 AM

    Paul Batum's Avatar

    Given that you don't seem to be getting errors if you change some of the
    table names, I would be exporting my mappings to xml and eyeballing them for
    issues.

    See:
    http://wiki.fluentnhibernate.org/Fluent_configuration#Exporting_mappings

  2. 3 Posted by barrett on 07 Jul, 2010 03:10 PM

    barrett's Avatar

    Paul,

    I actually solved my issue. It was caused by me incorrectly defining the assembly in the call to Fluent Configure.

    I am running into a separate problem now of :
    IndexOutOfRangeException : Invalid index 3for this SqlParameterCollection with Count=3

    I know that it is related to me creating multiple associations.
    As described in the following posts:

    http://devlicio.us/blogs/derik_whittaker/archive/2009/03/19/nhibern...
    http://stackoverflow.com/questions/2418926/fluentnhibernate-mapping...

    I do not seem to be able to figure it out.

    My Mappings have changed since the first post and are now:

    public PrivateProjectionAssumptionsMap()
       {
           Table("private_projection_assumptions");
           SchemaAction.None();
           Id(x => x.private_projection_assumption_id).Column("private_projection_assumption_id");
           Map(x => x.investment_vehicle_portfolio_id).Column("investment_vehicle_portfolio_id");
           Map(x => x.multiple).Column("multiple");
           Map(x => x.percent_drawn).Column("drawn");
           HasMany<PrivateEquityRateAssumptions>(x => x.RateAssumptions)
               .KeyColumn("private_projection_assumption_id")
               .Cascade.All();
       }
    
    public PrivateEquityRateAssumptionsMap()
        {
            Table("private_projection_rates");
            SchemaAction.None();
            Id(x => x.private_projection_rate_id).Column("private_projection_rates_id");
            Map(x => x.year_id).Column("year_id");
            Map(x => x.projected_call_rate).Column("projected_call_rate");
            Map(x => x.projected_distribution_rate).Column("projected_call_rate");
            References<PrivateProjectionAssumptions>(x => x.FundAssumptions, "private_projection_assumption_id");
        }
    

    Any thoughts?

  3. 4 Posted by barrett on 07 Jul, 2010 03:21 PM

    barrett's Avatar

    Yeah, I am dumb.

    I map "projected_call_rate" twice.

    You guys can probably mark this resolved as my entity round trip tests are passing now.

  4. Paul Batum closed this discussion on 11 Jul, 2010 06:11 AM.

Comments are currently closed for this discussion. You can start a new one.