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(IsPrimar yKey = true, Name = "customer_i d")]
public int CustomerID { get; set; }
private EntitySet<Trans action_Transact ions;
[Association(Sto rage = "_Transactions" ,
OtherKey="Custo merID")]
public EntitySet<Trans actionTransacti ons
{
get { return this._Transacti ons; }
set { this._Transacti ons.Assign(valu e); }
}
public Customer()
{
this._Transacti ons = new EntitySet<Trans action>();
}
}
[Table(Name = "custtran")]
public class Transaction
{
[Column(Name = "compno", DbType="smallin t")]
public int CompanyNumber { get; set; }
[Column(IsPrimar yKey = true, Name = "tran_no")]
public int TranNo { get; set; }
[Column(Name = "customer_i d")]
public int CustomerID { get; set; }
[Column(Name = "tran_type" , DbType="char(4) ")]
public string TranType { get; set; }
private EntityRef<Custo mer_Customer;
[Association(Sto rage = "_Customer" , ThisKey = "CustomerID ")]
public Customer Customer
{
get { return this._Customer. Entity; }
set { this._Customer. Entity = value; }
}
private EntitySet<Trans actionType_Tran sactionTypes;
[Association(Nam e="Custtran_Tra nType", Storage =
"_TransactionTy pes", IsForeignKey=tr ue, OtherKey = "TranType")]
public EntitySet<Trans actionTypeTrans actionTypes
{
get { return this._Transacti onTypes; }
set { this._Transacti onTypes.Assign( value); }
}
public Transaction()
{
this._Customer = default(EntityR ef<Customer>);
this._Transacti onTypes = new EntitySet<Trans actionType>();
}
}
[Table(Name = "tran_type" )]
public class TransactionType
{
[Column(Name = "compno", DbType = "smallint")]
public int CompanyNumber { get; set; }
[Column(IsPrimar yKey = true, Name = "tran_type" ,
DbType="char(4) ")]
public string TranType { get; set; }
[Column(Name = "descriptio n", DbType="Varchar (50)")]
public string Description { get; set; }
private EntityRef<Trans action_Transact ion;
[Association(Nam e="Custtran_Tra nType", Storage =
"_Transacti on", ThisKey = "TranType")]
public Transaction Transaction
{
get { return this._Transacti on.Entity; }
set { this._Transacti on.Entity = value; }
}
public TransactionType ()
{
this._Transacti on = default(EntityR ef<Transaction> );
}
}
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(IsPrimar yKey = true, Name = "customer_i d")]
public int CustomerID { get; set; }
private EntitySet<Trans action_Transact ions;
[Association(Sto rage = "_Transactions" ,
OtherKey="Custo merID")]
public EntitySet<Trans actionTransacti ons
{
get { return this._Transacti ons; }
set { this._Transacti ons.Assign(valu e); }
}
public Customer()
{
this._Transacti ons = new EntitySet<Trans action>();
}
}
[Table(Name = "custtran")]
public class Transaction
{
[Column(Name = "compno", DbType="smallin t")]
public int CompanyNumber { get; set; }
[Column(IsPrimar yKey = true, Name = "tran_no")]
public int TranNo { get; set; }
[Column(Name = "customer_i d")]
public int CustomerID { get; set; }
[Column(Name = "tran_type" , DbType="char(4) ")]
public string TranType { get; set; }
private EntityRef<Custo mer_Customer;
[Association(Sto rage = "_Customer" , ThisKey = "CustomerID ")]
public Customer Customer
{
get { return this._Customer. Entity; }
set { this._Customer. Entity = value; }
}
private EntitySet<Trans actionType_Tran sactionTypes;
[Association(Nam e="Custtran_Tra nType", Storage =
"_TransactionTy pes", IsForeignKey=tr ue, OtherKey = "TranType")]
public EntitySet<Trans actionTypeTrans actionTypes
{
get { return this._Transacti onTypes; }
set { this._Transacti onTypes.Assign( value); }
}
public Transaction()
{
this._Customer = default(EntityR ef<Customer>);
this._Transacti onTypes = new EntitySet<Trans actionType>();
}
}
[Table(Name = "tran_type" )]
public class TransactionType
{
[Column(Name = "compno", DbType = "smallint")]
public int CompanyNumber { get; set; }
[Column(IsPrimar yKey = true, Name = "tran_type" ,
DbType="char(4) ")]
public string TranType { get; set; }
[Column(Name = "descriptio n", DbType="Varchar (50)")]
public string Description { get; set; }
private EntityRef<Trans action_Transact ion;
[Association(Nam e="Custtran_Tra nType", Storage =
"_Transacti on", ThisKey = "TranType")]
public Transaction Transaction
{
get { return this._Transacti on.Entity; }
set { this._Transacti on.Entity = value; }
}
public TransactionType ()
{
this._Transacti on = default(EntityR ef<Transaction> );
}
}