Hi all,
I've been doing some reading about Linq to Sql and have been trying
some stuff out for myself to aid my learning, but I'm having a bit of
a problem trying to create an association between 2 entity classes.
The problem seems to be caused when the primary key is not an integer.
My code is below. I'm trying to link my custtran table with my
tran_types table based using the tran_type primary key of the
tran_types table. When the column is a char(4) i get an exception when
trying to run the program (Unable to cast object of type
'System.Int32' to type 'System.String'), if I change the column to be
a integer, the code works fine.
Just wondered if anyone has any idea what is wrong?
[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<TransactionTransactions
{
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<TransactionTypeTransactionTypes
{
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>);
}
}