Looking for C# Automapping example project that works

481 views
Skip to first unread message

tbushell

unread,
Nov 2, 2009, 4:02:52 PM11/2/09
to Fluent NHibernate
Can anyone point me to (or email me the file(s)) for a complete,
running sample project, written in C#, that demonstrates
Automapping?

Even better would be one that also demonstrates automapping an IList
property.

I've only found two so far:

- one that uses obsolete syntax

- one that's written in VB

I'd love to use Automapping on my project, but the near total lack of
good sample code and documentation for this technology is really
making me wonder if it's anywhere near ready for production use...

Thanks,

-Tom Bushell

James Gregory

unread,
Nov 2, 2009, 4:29:28 PM11/2/09
to fluent-n...@googlegroups.com

tbushell

unread,
Nov 2, 2009, 5:01:29 PM11/2/09
to Fluent NHibernate
Thanks James - I'm aware of that page, but my experience over the last
few days with trying to pull various NHibernate code fragments into a
working whole has been notably unsuccessful.

For developers who are new to the whole process such as myself, we
really need to start with something that already works. Otherwise,
it's been my experience that I just get a seemingly never ending
series of exceptions, with no idea what they mean, or how to fix them.

Something like the Examples.FirstProject, but with Automapping - would
be great - I'm surprised someone has not already added it to the
source. Or is this just too new for that level of sample code?

-Tom

On Nov 2, 4:29 pm, James Gregory <jagregory....@gmail.com> wrote:
> http://wiki.fluentnhibernate.org/Auto_mapping

James Gregory

unread,
Nov 2, 2009, 5:10:18 PM11/2/09
to fluent-n...@googlegroups.com
Automapping has existed for as long as fluent nhibernate has, there's no new about it.

If you're having issues, you should ask for help. We're here to help. It's much easier for us to answer a quick question on the mailing list than it is to create another example project. Sure, we should do that, but when our time is limited and it's between fixing a bug or creating more examples, bugs will always win. That being said, I truly don't believe it's that difficult to use it. I'm interested to know what troubles you had.

Getting the Example.FirstProject to use the automapper should be as simple as changing the CreateSessionFactory method to be something like this:

private static ISessionFactory CreateSessionFactory()
{
  var mappings = AutoMap.AssemblyOf<Product>()
                           .Where(t => t.Namespace == "Examples.FirstProject.Entities");
    
  return Fluently.Configure()
    .Database(SQLiteConfiguration.Standard
      .UsingFile(DbFile))
    .Mappings(m =>
      m.AutoMappings.Add(mappings))
    .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();

Mikael Henriksson

unread,
Nov 2, 2009, 5:19:58 PM11/2/09
to fluent-n...@googlegroups.com
I am sorry if I offend someone but trying to ask for a solution is like trying to ask someone to do it for you. You'll get by with a bunch of imagination and a lot of google-foo!!!!

Hudson Akridge

unread,
Nov 2, 2009, 5:21:18 PM11/2/09
to fluent-n...@googlegroups.com
As with any open source system, if you don't like something about the way it works, the documentation, examples, etc. Please feel free to contribute. I have an older example for automapping in C#, but it's out of date and is not current with version 1 syntax, so wouldn't be too useful I'd imagine.

Understand, that coming to us, complaining about how something doesn't have the exact examples you need, or as verbose/descriptive documentation as you'd like, is rude ;) We (and by We, I really mean mostly James and Paul) spend a great deal of time improving this free, OSS thing that's available to everyone, comes with a wiki, an API, examples, and a very active user group available and willing to answer all sorts of questions.

No offense intended, but you're a developer. There's *more* than enough on the wiki and in the API documentation for you. Figure it out.

Mikael Henriksson

unread,
Nov 2, 2009, 5:25:10 PM11/2/09
to fluent-n...@googlegroups.com
I couldn't agree more. I do complain more about Fluent than NHibernate (I've created some patches for NH) but that's just because the fluent guys have not done it for so long =:)

tbushell

unread,
Nov 2, 2009, 6:57:43 PM11/2/09
to Fluent NHibernate


On Nov 2, 5:10 pm, James Gregory <jagregory....@gmail.com> wrote:
> Automapping has existed for as long as fluent nhibernate has, there's no *
> new* about it.

My mistake - it's ALL new to me! Maybe that's part of my problem - I
don't have any previous experience with NHibernate in any form.

I don't mean to seem ungrateful - I'm just trying to get my project
done. But it's been a very frustrating couple of days...

>
> If you're having issues, you should ask for help. We're here to help. It's
> much easier for us to answer a quick question on the mailing list than it is
> to create another example project. Sure, we should do that, but when our
> time is limited and it's between fixing a bug or creating more examples,
> bugs will always win.

I understand.

> That being said, I truly don't believe it's that
> difficult to use it. I'm interested to know what troubles you had.
>
> Getting the Example.FirstProject to use the automapper should be as simple
> as changing the CreateSessionFactory method to be something like this:
>
> private static ISessionFactory CreateSessionFactory()
> {
>   var mappings = AutoMap.AssemblyOf<Product>()
>                            .Where(t => t.Namespace ==
> "Examples.FirstProject.Entities");
>
>   return Fluently.Configure()
>     .Database(SQLiteConfiguration.Standard
>       .UsingFile(DbFile))
>     .Mappings(m =>
>       m.AutoMappings.Add(mappings))
>     .ExposeConfiguration(BuildSchema)
>     .BuildSessionFactory();

Thanks for this, it was very helpful, and it _almost_ works. And for
the first time, the error message was clear enough that I was able to
guess at a brute force workaround for the problem.

I'm not trying to be a wise ass, but as an exercise in putting
yourself in the first time user's shoes, try it yourself. And I'd be
interested to know what the "proper" solution to the cascading saves
problem is.

If you have the time and patience, I'd like to work through this with
you, and would be happy to post it back to the main source when it's
done.

-Tom

Paul Batum

unread,
Nov 2, 2009, 7:04:55 PM11/2/09
to fluent-n...@googlegroups.com
Well I maintain a testing project for diagnosing FNH issues and its available on GitHub. The code changes every time I have to investigate a different issue but I can tag versions and expose them for others to download.

The project is here:
http://github.com/paulbatum/Fluent-NH-Test-Bed/

I've tagged a very simple automapping example that should work straight out of the box here (theres a download link if you don't want to pull it down with git):
http://github.com/paulbatum/Fluent-NH-Test-Bed/tree/auto-version-col

Let me know if it helps!

Paul Batum

tbushell

unread,
Nov 3, 2009, 10:14:16 AM11/3/09
to Fluent NHibernate
> I've tagged a very simple automapping example that should work straight out
> of the box here (theres a download link if you don't want to pull it down
> with git):http://github.com/paulbatum/Fluent-NH-Test-Bed/tree/auto-version-col

Thanks Paul - it was what I was looking for.

However, when I run it, I get the following exception in
CreateSessionFactory:

An unhandled exception of type
'FluentNHibernate.Cfg.FluentConfigurationException' occurred in
FluentNHibernate.dll

Additional information: An invalid or incomplete configuration was
used while creating a SessionFactory. Check
PotentialReasons collection, and InnerException for more detail.

Do you get the same behavior?

-Tom


James Gregory

unread,
Nov 3, 2009, 10:53:19 AM11/3/09
to fluent-n...@googlegroups.com
What does the inner exception say?

tbushell

unread,
Nov 3, 2009, 12:09:56 PM11/3/09
to Fluent NHibernate
"Could not find a part of the path 'C:\\Documents and Settings\\softdev
\\Desktop\\paulbatum-Fluent-NH-Test-Bed-108b9f8\\paulbatum-Fluent-NH-
Test-Bed-108b9f8\\Console\\bin\\Debug\\mappings\
\AutoMappings.hbm.xml'."

The "mappings" directory does not exist on my PC.

Is there a way to configure NHibernate to auto create that directory,
or should I just do it in my own code?

Also, I got the inner exception message by putting a "try-catch" block
around the problem code, and browsing the Exception parameter. Is
there a better way to do this?

Thanks,

-Tom

Also,
> > -Tom- Hide quoted text -
>
> - Show quoted text -

Hudson Akridge

unread,
Nov 3, 2009, 12:26:43 PM11/3/09
to fluent-n...@googlegroups.com
Replace line 38 of Setup.cs in the Console application with:
m.AutoMappings.ExportTo(string.Empty);

That should export the mappings to the root folder. Although it may be worthwhile to create a mappings directory to keep things organized with this example.

malovicn

unread,
Nov 3, 2009, 2:44:23 PM11/3/09
to Fluent NHibernate
I've started a small project doing what you've asked for: small sample
demoing the fnh concepts "in action"
http://fnhsamples.codeplex.com/

I just started it (there would be much more code there in the future),
but it has already initial auto mapping example so go check it out and
I hope it could be of some help to you.

Nikola


On Nov 2, 11:19 pm, Mikael Henriksson <mik...@zoolutions.se> wrote:
> I am sorry if I offend someone but trying to ask for a solution is like
> trying to ask someone to do it for you. You'll get by with a bunch of
> imagination and a lot of google-foo!!!!
>
> On Mon, Nov 2, 2009 at 11:10 PM, James Gregory <jagregory....@gmail.com>wrote:
>
>
>
> > Automapping has existed for as long as fluent nhibernate has, there's no *
> > new* about it.
> >> >http://wiki.fluentnhibernate.org/Auto_mapping- Hide quoted text -

James Gregory

unread,
Nov 3, 2009, 2:46:12 PM11/3/09
to fluent-n...@googlegroups.com
Brilliant Nikola, now that's how it should be done; if you see something wrong, fix it! :)

I'll be sure to put a link up on the FNH site when I next get a minute.

Paul Batum

unread,
Nov 3, 2009, 4:46:40 PM11/3/09
to fluent-n...@googlegroups.com
Sorry about that mappings folder problem. There is code in there to create the folder but the version I tagged for you was before that code was added.

Very nice work Nikola, this will save me from having to link people to my test-bed.

tbushell

unread,
Nov 3, 2009, 5:35:15 PM11/3/09
to Fluent NHibernate
Nikola, that sounds terrific - I'm sure many people will benefit.

Have not downloaded it yet, because I've got some momentum again
(mainly thanks to Paul's test project), and am starting to think this
may work out after all.

But I'll take a look, and contribute if I can. One trivial possibility
- to create the mappings directory, I just added the following:

// Automapped XML files will be exported to project's
// ...\bin\x86\Debug\AutoMapExport directory
// See ".ExportTo()" below
const string autoMapExportDir = "AutoMapExport";
if (!Directory.Exists(autoMapExportDir))
Directory.CreateDirectory(autoMapExportDir);

Hope to add something more substantial in the future.

I also like that you are setting it up to work with SQL Server
Express, because that will allow us to browse the created tables with
VS2008's Server Explorer SQL Server Managment Studio, which are more
familiar than SQLite.

-Tom
> > >> >http://wiki.fluentnhibernate.org/Auto_mapping-Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages