0

I have seen a few questions like this on SO, but I still cannot get this to work in my code, despite my code looking like the accepted answers.

First, my mapping (condensed)

public SiteMap()
{
    Table("SiteDetail");

    Id(x => x.SiteId, "Id").GeneratedBy.Identity();
}

Then, the other table's structure

CREATE TABLE [dbo].[EmailConfig](
    [SiteID] [int] NOT NULL,
    [Active] [int] NULL,
    [HeaderText] [int] NULL,
    [TrailerText] [int] NULL,
    [BodyText] [int] NULL,
    [CopyEmailTo] [varchar](255) NULL,
CONSTRAINT [PK_EmailConfig] PRIMARY KEY CLUSTERED ([SiteID] ASC)) 

And here is my property in my object

public virtual bool EmailReceiptsEnabled { get; set; }

And here is the mapping to set this property

Map(x => x.EmailReceiptsEnabled)
    .Formula("(SELECT COUNT(e.SiteId) FROM EmailConfig e WHERE e.SiteId = SiteId AND e.Active = 1)");

When I comment out that property mapping, everything works fine. When I leave it in, my repository returns null when trying to select a single site. No error is thrown, other than the null reference exception on the page trying to access a property of the site object.

EDIT:

I have modified my solution to pull back an object like so:

public class EmailConfiguration : CoreObjectBase
{
    public virtual int SiteId { get; set; }
    public virtual int Active { get; set; }
}

With a property in my Site class

public virtual EmailConfiguration EmailConfiguration { get; set; }

And the mapping for the object

public class EmailConfigurationMap : ClassMap<EmailConfiguration>
{
    public EmailConfigurationMap()
    {
        Table("EmailConfig");

        Id(x => x.SiteId);

        Map(x => x.Active);
    }
}

And then the mapping in my site

References(x => x.EmailConfiguration, "Id");

And it produces an error

NHibernate.ObjectNotFoundException: No row with the given identifier exists

The issue is that EmailConfig contains no record until the client goes in to the app to configure their Email. That can't be changed, but I need my application to be able to use this data. Any ideas on how to map this thing?

Yet another edit

I was able to get it to work by changing the property to this

public virtual IList<EmailConfiguration> EmailConfigurations { get; set; }

And then implementing this complete disaster

HasMany<EmailConfiguration>(x => x.EmailConfigurations)
    .KeyColumn("SiteId")
    .Inverse();

I think I'll go cry quietly until I can figure out how to make this mess work the right way. Since the EmailConfig table uses the PK of my site table as its primary key, the only possibilities are to have 1 or no records. References fails on null with a hard error. HasOne doesn't work either (causes the program to not pull back my Site objects at all without any errors).

Josh
  • 16,286
  • 25
  • 113
  • 158
  • Before your edit how were you querying your Site entity. Was it off the SiteId? Did you take a look at the query it was running when you had the formula mapped via the log files or NHProf? I set up a scenario like this and I was doing query by example and that formula was screwing up the query for me. Also I think this truly is a one to one relationship since both tables have the same primary key. – Cole W Apr 14 '11 at 01:06
  • The site has a 4 character abbreviation that I was using to look up the site on. I use a class to define a linq expression that is then passed into my repository, which uses the expression with linq to nhibernate. The important thing is that the query works without the mapping, but doesn't return anything when the mapping is included. – Josh Apr 14 '11 at 13:15
  • Have you actually looked at the query that is hitting the database though when you have the formula mapped vs when you don't have it mapped? I don't think that your formula is what is necessarily causing your issues. It seems like the where clause is being generated differently when you have this mapped because you are getting a null object back. If it was the mapping that was incorrect it seems like the value of that property would be the only thing that was incorrect. – Cole W Apr 14 '11 at 13:46

0 Answers0