如何修改PetaPoco类以使用包含非数字列的Composite键?

时间:2012-06-23 11:29:33

标签: c# .net petapoco

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

ContractorId ......... INT ............. IDENTITY
ContractorName ........ Varchar(50)....... P.K
ContractorGrade ....... Varchar(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新记录的代码如下:

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

db.Insert(Contractor);

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

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

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

3 个答案:

答案 0 :(得分:7)

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

[PrimaryKey("ContractorName,ContractorGrade")]

如果你想要那里的标识栏,我不确定它是如何工作的。

答案 1 :(得分:2)

我必须进行以下更改才能支持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;
            }
        }

答案 2 :(得分:0)

我现在可以看到CompositeKeySuppport branch,所以我们很快就会在官方报告中得到支持,这意味着我们将获得NuGet的更新和内容。