Andriy Kizym
Andriy Kizym

Reputation: 1796

Fluent nHibernate dynamic property name

I want to make this mapping work:

    Map(x => x.First, "First_ID");
    Map(x => x.Second, "Second_ID")

    References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns("First_ID", "Second_ID")
        .LazyLoad();

It doesn't work when I want to insert the entity to the database. It says: Invalid Index N for this SqlParameterCollection with Count=N error

I've found an answer for the question here: http://devlicio.us/blogs/derik_whittaker/archive/2009/03/19/nhibernate-and-invalid-index-n-for-this-sqlparametercollection-with-count-n-error.aspx

But the answer says I need to remove below code, to make insert work

  Map(x => x.First, "First_ID");
  Map(x => x.Second, "Second_ID")

The problem is that I can't remove the mappings cause in that way my entity couldn't be fetched from the database. It than says that it cannot find First and Second columns in table. I understand why this happens but is there some way to solve the problem without removing the mappings? Just to change something in this code:

References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns("First_ID", "Second_ID")
        .LazyLoad();

For example not to use literals for column names, but some code that "knows" that in this mapping property First is mapped to "First_ID" without specifying explicitly the name of the column?

P.S. I've tried to use

References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns(x => x.First, x => x.Second)
        .LazyLoad();

It doesn't work.

The other solution for removed mappings is to change the query that gets the entity from db, but in this case i need to add extra join - it's stupid cause I have the column in my own table, why i need to make join and then add some restrictions when I don't exactly need this join.

Upvotes: 0

Views: 1292

Answers (1)

Vadim
Vadim

Reputation: 17965

You can specify that the properties should not be updateable by doing:

Map(x => x.First, "First_ID").Not.Update();
Map(x => x.Second, "Second_ID").Not.Update();

Or if the only reason you want to have these properties is to use them in queries. You can either do

Map(x => x.First, "First_ID").Access.None();
Map(x => x.Second, "Second_ID").Access.None();

Or more simply remove those properties and use good old fashion hbm files to map your entities and specify access="none" for your properties. That way they will be available to you through Criteria and HQL, but won't actually need to exist in your entity.

Upvotes: 2

Related Questions