Question

I have 2 tables (Labour and LabourPosition). Instead of having a fki to the LabourPosition table from the Labour table, I'd like to use an object (theLabourer.LabourPosition.Nameinstead of getLabourPosition(theLabourer.LabourPositionId).

I've read a few articles to do this but they don't seem to be working for me... the error I get when trying to view the value of the LabourPosition property is "Could not initialize proxy - no Session".

My mapping (p.s: LabourPosition does not have a fki back to Labour, one way only): LabourMap class:

References<LabourPosition>(x => x.LabourPosition, "LabourPositionsId");

And then obviously I've defined the property in the Labour entity as:

public virtual LabourPosition LabourPosition { get; set; }

Any ideas? Help would be appreciated!

Was it helpful?

Solution

From the fluent mapping documentation

HasOne / one-to-one

HasOne is usually reserved for a special case. Generally, you'd use a References relationship in most situations (see: I think you mean a many-to-one). If you really do want a one-to-one, then you can use the HasOne method.

HasOne(x => x.Cover);

If you want to use a reference (as your code suggests), you need to map both sides of the relationship. From the documentation:

HasMany / one-to-many

HasMany is probably the most common collection-based relationship you're going to use. HasMany is the "other side" of a References relationship, and gets applied on the "one side" (one author has many books). Now let's map the author side of the relationship we started above. We need to join into the books table returning a collection of any books associated with that author.

public class Author { public IList Books { get; set; } } We use the HasMany method in the AuthorMap constructor to map this side of the relationship:

HasMany(x => x.Books); As with References, the foreign-key defaults to Author_id, and you can override it with the KeyColumn method or change the default behaviour with a Convention.

There are a few different types of collections you can use, and they're all available under the HasMany call.

Why do you need a one-one relationship, instead of just keeping all the columns in a single table?

OTHER TIPS

In your mapping try

References(x => x.LabourPosition, "LabourPositionsId");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top