schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
848 stars 302 forks source link

Guid PrimaryKey, with auto increment on another int field? #608

Closed ruant closed 4 years ago

ruant commented 4 years ago

I'm having problems to get this to work.

    [TableName(OrderSql.TableName)]
    [PrimaryKey(OrderSql.Id, AutoIncrement = false)]
    [ExplicitColumns]
    public class Order : DatabaseModelBase
    {
        [Column(OrderSql.Id)]
        public Guid Id { get; set; }

        [Column(OrderSql.OrderIdRef)]
        public int OrderIdRef { get; set; }

        [Column(OrderSql.MemberId)]
        public Guid MemberId { get; set; }
    }

The Id field is the PrimaryKey and is Guid. The OrderIdRef is set to auto increment in the database.

But I'm getting the following error: Cannot insert explicit value for identity column in table 'Order' when IDENTITY_INSERT is set to OFF.

schotime commented 4 years ago
[TableName(OrderSql.TableName)]
[PrimaryKey(OrderSql.Id, AutoIncrement = false)]
[ExplicitColumns]
public class Order : DatabaseModelBase
{
    [Column(OrderSql.Id)]
    public Guid Id { get; set; }

    [ResultColumn(OrderSql.OrderIdRef)]
    public int OrderIdRef { get; set; }

    [Column(OrderSql.MemberId)]
    public Guid MemberId { get; set; }
}

Try using [ResultColumn] on that one so it only gets populated but not updated.

ruant commented 4 years ago

Of course! Why didn't I think of that attribute.
Works like a charm.

Cheers @schotime 🥂

ruant commented 4 years ago

I ended up using [ComputedColumn] since I want the SQL query being built to include the field in the SELECT.

Ref: https://github.com/schotime/NPoco/wiki/Mapping