C# – Linq to Sql entity associations (non integer primary keys)

clinq-to-sql

I've been playing around with Linq to Sql, but i've come accross a problem when defining associations between entities.

I have 2 tables, 1 is a table containing customer transactions and the other is a lookup table containing transaction types. For example, a transaction can be type 'CASH' and the value in the lookup table will have a primary key of 'CASH' and a description 'Cash Payment'.

When I run my test program, I get an "Unable to cast object of type 'System.Int32' to type 'System.String'" error, if I change the primary key in the transaction and lookup tables to an integer, the program works as expected.

I'm probrably missing something fundamental, but i'm not sure what. Any help appreciated.

Here's the code I'm using to map my tables:

[Table(Name = "customer")]
public class Customer
{
    [Column(IsPrimaryKey = true, Name = "customer_id")]
    public int CustomerID { get; set; }

    private EntitySet<Transaction> _Transactions;

    [Association(Storage = "_Transactions", OtherKey="CustomerID")]
    public EntitySet<Transaction> Transactions
    {
        get { return this._Transactions; }
        set { this._Transactions.Assign(value); }
    }

    public Customer()
    {
        this._Transactions = new EntitySet<Transaction>();
    }
}

[Table(Name = "custtran")]
public class Transaction
{
    [Column(Name = "compno", DbType="smallint")]
    public int CompanyNumber { get; set; }

    [Column(IsPrimaryKey = true, Name = "tran_no")]
    public int TranNo { get; set; }

    [Column(Name = "customer_id")]
    public int CustomerID { get; set; }

    [Column(Name = "tran_type", DbType="char(4)")]
    public string TranType { get; set; }

    private EntityRef<Customer> _Customer;

    [Association(Storage = "_Customer", ThisKey = "CustomerID")]
    public Customer Customer
    {
        get { return this._Customer.Entity; }
        set { this._Customer.Entity = value; }
    }

    private EntitySet<TransactionType> _TransactionTypes;
    [Association(Name="Custtran_TranType", Storage = "_TransactionTypes", IsForeignKey=true,  OtherKey = "TranType")]
    public EntitySet<TransactionType> TransactionTypes
    {
        get { return this._TransactionTypes; }
        set { this._TransactionTypes.Assign(value); }
    }

    public Transaction()
    {
       this._Customer = default(EntityRef<Customer>);
       this._TransactionTypes = new EntitySet<TransactionType>();
    }
}

[Table(Name = "tran_type")]
public class TransactionType
{
    [Column(Name = "compno", DbType = "smallint")]
    public int CompanyNumber { get; set; }

    [Column(IsPrimaryKey = true, Name = "tran_type", DbType="char(4)")]
    public string TranType { get; set; }

    [Column(Name = "description", DbType="Varchar(50)")]
    public string Description { get; set; }

    private EntityRef<Transaction> _Transaction;
    [Association(Name="Custtran_TranType", Storage = "_Transaction", ThisKey = "TranType")]
    public Transaction Transaction
    {
        get { return this._Transaction.Entity; }
        set { this._Transaction.Entity = value; }
    }

    public TransactionType()
    {
        this._Transaction = default(EntityRef<Transaction>);
    }
}

Best Answer

Ignore me, I've answered my own question.

private EntitySet<TransactionType> _TransactionTypes;
    [Association(Name="Custtran_TranType", Storage = "_TransactionTypes", IsForeignKey=true, ThisKey="TranType", OtherKey = "TranType")]
    public EntitySet<TransactionType> TransactionTypes
    {
        get { return this._TransactionTypes; }
        set { this._TransactionTypes.Assign(value); }
    }

Added ThisKey to the Association and it now works fine.

Related Topic