如何修改 PetaPoco 类以使用由非数字列组成的复合键



我有一个包含以下列的表格:

承包商编号国际...身份
承包商名称 ...瓦尔查尔(50)P.K
承包商级 ...瓦尔查尔(3)P.K

PetaPoco T4 模板生成的类如下所示:

[TableName("contractor_master")]
[PrimaryKey("contractorname", autoIncrement=false)]
[ExplicitColumns]
public partial class contractor_master : TubewellRepo.Record<contractor_master>  
{
    [Column] 
    public int contractorid 
    { 
        get
        {
            return _contractorid;
        }
        set
        {
            _contractorid = value;
            MarkColumnModified("contractorid");
        }
    }
    int _contractorid;
    [Column] 
    public string contractorname 
    { 
        get
        {
            return _contractorname;
        }
        set
        {
            _contractorname = value;
            MarkColumnModified("contractorname");
        }
    }
    string _contractorname;
    [Column] 
    public string contractorgrade 
    { 
        get
        {
            return _contractorgrade;
        }
        set
        {
            _contractorgrade = value;
            MarkColumnModified("contractorgrade");
        }
    }
    string _contractorgrade;
  }

插入新记录的代码如下:

// Insert a record
var Contractor=new contractor_master();
Contractor.contractorname = "Super Borewells";
Contractor.contractorgrade = "A";
db.Insert(Contractor);

在类代码的第二行中,我想知道如何提及复合键,即(ConcontractName + ConcontractGrade)。

其次,它没有插入记录,因为它需要一个 Id 列。即使 ConcontractId 是 IDENTITY,它也不是主键。

它没有插入新记录并给出错误,因为它在 IDENTITY 列中插入 0。

我的分支在这里:https://github.com/schotime/petapoco通过指定 PrimaryKey 属性来支持复合主键,如下所示:

[PrimaryKey("ContractorName,ContractorGrade")]

如果您也希望那里的身份列也在那里,我不确定它将如何工作。

我必须进行以下更改以支持IsNew()

// Check if a poco represents a new record
        public bool IsNew(string primaryKeyName, object poco)
        {
            var pd = PocoData.ForObject(poco, primaryKeyName);
            object pk;
            PocoColumn pc;
            if (pd.Columns.TryGetValue(primaryKeyName, out pc))
            {
                pk = pc.GetValue(poco);
            }
#if !PETAPOCO_NO_DYNAMIC
            else if (poco.GetType() == typeof(System.Dynamic.ExpandoObject))
            {
                return true;
            }
#endif
            else if (primaryKeyName.Contains(","))
            {
                return primaryKeyName.Split(',')
                    .Select(pkPart => GetValue(pkPart, poco))
                    .Any(pkValue => IsDefaultOrNull(pkValue));
            }
            else
            {
                pk = GetValue(primaryKeyName, poco);
            }
            return IsDefaultOrNull(pk);
        }
        private static object GetValue(string primaryKeyName, object poco)
        {
            object pk;
            var pi = poco.GetType().GetProperty(primaryKeyName);
            if (pi == null)
                throw new ArgumentException(
                    string.Format("The object doesn't have a property matching the primary key column name '{0}'",
                                  primaryKeyName));
            pk = pi.GetValue(poco, null);
            return pk;
        }
        private static bool IsDefaultOrNull(object pk)
        {
            if (pk == null)
                return true;
            var type = pk.GetType();
            if (type.IsValueType)
            {
                // Common primary key types
                if (type == typeof(long))
                    return (long)pk == default(long);
                else if (type == typeof(ulong))
                    return (ulong)pk == default(ulong);
                else if (type == typeof(int))
                    return (int)pk == default(int);
                else if (type == typeof(uint))
                    return (uint)pk == default(uint);
                else if (type == typeof(Guid))
                    return (Guid)pk == default(Guid);
                // Create a default instance and compare
                return pk == Activator.CreateInstance(pk.GetType());
            }
            else
            {
                return pk == null;
            }
        }

我可以看到现在有 CompositeKeySuppport 分支,所以我们很有可能很快就会在官方存储库中支持它,这意味着我们将获得 NuGet 更新和其他东西。

最新更新