jgauffin / Griffin.Framework

Application framework for Business Applications
http://griffinframework.net
168 stars 63 forks source link

connection.Insert returning null - PK problem? #73

Closed displaced closed 8 years ago

displaced commented 8 years ago

Hi,

I've got a simple POCO ('Model.Client') which matches exactly my database fields. The mapper for that is defined as:

public class ClientMapper : CrudEntityMapper<Model.Client>
{
    public ClientMapper() : base("Clients")
    {
    }
}

...which I believe is the correct way to define a mapper if the model properties match the database fields.

Now, if I insert an instance of this model as so:

var ret = _conn.Insert(client);

...the row is added to the database table correctly, but ret is null. I believe this should either be the identity of the newly-inserted row, or an instance of the model that was inserted with the .Id property set appropriately.

My database correctly has an auto-incrementing primary key column named Id, and my entity object has a matching int Id { get; set; } property defined.

No exceptions appear to be thrown. I'm using the latest version of Griffin.Framework as available on NuGet.

It's almost as if the mapper hasn't picked up the presence of the Id field. Is there any way to see what's going on inside the mapper during use?

Any advice greatly appreciated!

jgauffin commented 8 years ago

can you post the SQL for your table?

jgauffin commented 8 years ago

sqlite or sql server?

jgauffin commented 8 years ago

Not all tables use auto incremented primary keys and therefore it's not turned on per default.

To allow Griffin.Framework to pick up the generated value you need to configure it:

public class ClientMapper : CrudEntityMapper<Model.Client>
{
    public ClientMapper() : base("Clients")
    {
        Property(x => x.Id)
            .PrimaryKey(true); //true = autoinremented
    }
}

reopen if that's not your issue.