NPoco icon indicating copy to clipboard operation
NPoco copied to clipboard

Documentation - string primary key

Open openidauthority opened this issue 8 years ago • 2 comments

If you have a string for a primary key, like this:

[PrimaryKey("Id", AutoIncrement = false)]
public class MyRecord
{
    public string Id { get; set; }
    //...
}

some operations will interpret your id as a SQL statement. For example:

_db.Delete<MyRecord>(id); // throws a sql exception

It would be nice to document that a string key may need to be cast to an object in order for the right method overload to be called:

_db.Delete<MyRecord>((object)id);

openidauthority avatar Feb 20 '18 09:02 openidauthority

Hi, it's possible to set a string as primary key? i've been trying but i didn't figured it out. The way you do doesn't work.

1151136 avatar Jun 03 '19 14:06 1151136

[PrimaryKey("Id", AutoIncrement = false)] public class MyRecord { public string Code { get; set; } //... }

MyRecord t= new MyRecord() t.Code = "xx"; _db.Update(t); // throws a sql exception Insert no problem,but update PrimaryKey Exception。

Fix method NPoco.TableInfo public static TableInfo FromPoco(Type t)

      //Add New Code Fix PrimaryKey Exception
        if (tableInfo.PrimaryKey.Contains("false"))
        {
            tableInfo.AutoIncrement = false;
        }
        //新增修正代码
        tableInfo.PrimaryKey = tableInfo.PrimaryKey.Replace(",AutoIncrement=false", "");
        return tableInfo;

harme2019 avatar Oct 21 '19 06:10 harme2019