8

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

ContractorId .... INT .... IDENTITY
ContractorName .... Varchar(50) .... PK
ContractorGrade ... .... Varchar(3) .... PK

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);

在 Class Code 的第二行,我想知道如何提及一个 Composite Key,即 (ContractorName + ContractorGrade)。

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

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

4

3 回答 3

7

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

[PrimaryKey("ContractorName,ContractorGrade")]

如果您还想要标识列,我不确定它将如何工作。

于 2012-06-27T08:26:52.123 回答
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;
            }
        }
于 2013-01-17T03:23:43.393 回答
0

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

于 2016-09-24T11:48:05.973 回答