Fluent PetaPoco – External Mappings

Update 20/Nov/2011: This has been now added to my master branch and is available here for download as of https://github.com/schotime/PetaPoco/downloads

The other day I set out to build another way to configure the mappings for PetaPoco. Here is how you currently configure a mapping using attributes.

    public class attribpoco
        public int Id { get; set; }
        public string Name { get; set; }
        public string Result { get; set; }

        public int IgnoreColumn { get; set; }

This is pretty easy and requires only a couple of attributes. After you do a few of these, sometimes I wish I could set all the mappings in one place, so here is the same mappings but with my new configuration. Method 1.

    public class MyMappings : PetaPocoMappings
        public MyMappings()
                .PrimaryKey(x => x.Id)
                .Columns(x =>
                                 x.Ignore(y => y.IgnoreColumn);

So you can define multiple mappings here with new For<> statements. If you want to be more explicit and have one mapping class per mapping you can do the following. Method 2.

    public class AttribPocoMap : PetaPocoMap<attribpoco>
        public AttribPocoMap()
            PrimaryKey(x => x.Id);
            Columns(x =>
                            x.Ignore(y => y.IgnoreColumn);

Both ways of mapping have the same API, so they can be interchanged, however when hooking the mappings into PetaPoco, things differ a little.

You need to configure the mappings just once in your application so a good place to do that in a web application is the Application_Start() method in the global.asax.cs. Here is how you do this for the first method of mapping configuration.

PetaPoco.FluentMappings.Configuration.Configure(new MyMappings());

For method 2 you have to be a little more explicit at the moment. For each mapping you need to include it as a parameter to Configure.

PetaPoco.FluentMappings.Configuration.Configure(new AttribPocoMap(),…);

Also, just because you have used the fluent mappings for one class, you can use the attribute mapping style at any time as it will automatically default back to the attribute mappings if you don’t have a fluent mapping configured. You also don’t have to define all the columns. By default all columns are mapped unless you are using explicit mappings or you have used the Ignore or Result column like above.

Currently all features supported by attributes in my branch are supported in the fluent mappings. eg. Sequences, Explicit Columns, Composite Keys, Versioning, Ignore Columns and Result Columns.

If you would like to give it a try you can download the my PetaPoco branch here:


Let me know what you think.


8 Responses to Fluent PetaPoco – External Mappings

  1. Soe says:

    Hi Adam,
    Great Post.
    I followed your download link and downloaded the project. But I couldn’t find FluentMapping.cs. I tried your Fluent Mapping as you mentioned above. Even though I can Insert a record but cannot make query. I got following error.

    StackTrace = ” at PetaPoco.Database.PocoData.GetFactory(String sql, String connString, Boolean ForceDateTimesToUtc, Int32 firstColumn, Int32 countColumns, IDataReader r) in D:\\Projects\\PCS4Research\\PetaPoco\\PetaPocoTest\\PetaPocoTest\\Models\\PetaPoco.cs:line 2360…

    Here’s my code to query.

    IEnumerable scList = db.Query(“SELECT * FROM Commodity”);

    Please kindly point me right direction.

  2. Leon says:

    I was just about to implement it myself before I found your post.

  3. I really like the idea of fluent mappings, but the ExternalConfig branch seems to be rotting. Wasn’t it such a good idea after all? I’ll merge it locally into my copy of your PetaPoco master branch to see if it works.

  4. Schotime says:

    I’m gonna try and put it into my master branch today.

  5. I tryed your solution and I like it, but there is a bug or a feature missing :

    For().Columns(c => c.Column(cl => cl.SubProperty.SubSubField, “AField”))

    will raise the exception “System.Reflection.targetException : the object doesn’t match target type”

    I’m trying to figure out how I can handle it

  6. biju says:

    Can you please tell me why petaPoco returns a ‘null’ for a nullable date field even if my MYSQL table have a valid date ?

    I am using Mysql 5.5 , Mysql.Data 6.5.4

  7. Schotime says:

    Probably because your column doesn’t match your POCO.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>