Hudson,

Thanks for your suggestions.  I had considered this before, but in my
case this won't work because we have a UserCounties table that
determines which counties a User has access (it is used in a Many to
Many relationship, so this table is not mapped to an object).  This
table actually has the CountyID in it, but does not contain the
StateName or CountyName, so I am forced to use CountyID for the
primary key.

So, is my only option to create a new County object, say ProjectCounty
that maps by compositeID, and keep the existing County object?  It
would break a few LINQ queries, but I can refactor them to compare
against StateName and CountyName instead of just ID.

Thanks again,

-Dallas

On Mar 30, 3:05 pm, Hudson Akridge <[email protected]> wrote:
> Technically, you can't. Not directly like that. This is a NHibernate
> Limitation, not a Fluent limitation.
>
> What you need to do to get around it, is to set up a CompositeID
> mapping in your  CountyMap, this would replace your "CountyID". The
> method is .UseCompositeID() on the ClassMap for CountyMap, and map
> your StateCode and Name as the fields for the composite key.
>
> The reason is simple. A one-to-many is the reverse side of the many-to-
> one. This relationship, inherently through NHibernate, will use the ID
> mapping of the entity containing the one-to-many when saving/looking
> up data from the many-to-one side.
>
> What your CountyMap is technically missing, is:
> HasMany(x=> x.Projects)
> With whatever the name of your collection is. If you don't want the
> collection of projects on your County class, then you can still do
> this, but you'll definitely need the CompositeID mapping instead of ID
> mapping for County.
>
> On Mar 30, 1:09 pm, Dallas <[email protected]> wrote:
>
> > Hey guys,
>
> > I have an issue where our legacy database model isn't exactly
> > normalized- we have a list of Projects that need a County associated
> > with them.  In the Counties database we have a CountyID for the
> > Primary Key, but in the Projects database we do not-- I need to map
> > the County and Location_State columns from the Projects database
> > against CountyName and StateAlphaCode in the Counties database.
>
> > In ProjectMap.CS I have:
> > References<County>(x => x.County, "Location_State").PropertyRef(x =>
> > x.StateCode);
>
> > CountyMap if you need it:
> > public CountyMap()
> >         {
> >             WithTable("Counties");
>
> >             Id(x => x.ID, "CountyID")
> >                 .WithUnsavedValue(0)
> >                 .GeneratedBy.Assigned();
>
> >             Map(x => x.Name, "CountyName");
> >             Map(x => x.StateCode, "StateAlphaCode");
> >         }
>
> > This is the generated SQL:
> > FROM Projects this_ left outer join Counties county1_ on
> > this_.Location_State=county1_.StateAlphaCode
>
> > I need it to have the CountyName in there too, so this is what I need
> > it to generate this:
> > FROM Projects this_ left outer join Counties county1_ on
> > this_.Location_State=county1_.StateAlphaCode AND
> > this_.County=county1_.CountyName.
>
> > I need a way to do 2 PropertyRef, but that fails on a Reference
> > already defined.  So the question is, how to I map a Many to 1 against
> > an object where I'm NOT using its ID?
>
> > Thank you in advance,
>
> > -Dallas
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to