473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Linq to Sql problem

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>);
}
}
Oct 26 '08 #1
0 1100

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Veeneman | last post by:
I've been hearing a lot about LINQ in connection with Orcas, the next release of VS.NET. Micorosoft touts LINQ as the Next Big Breakthrough, but it looks to me like further muddying of application...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
22
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast....
6
by: Dmitry Perets | last post by:
Hello, I am trying to work with MS SQL Server 7 from the release version of Visual Studio 2008 + LINQ to SQL. And the problem is that the LINQ to SQL designer doesn't accept my tables saying...
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
4
by: Jacek Jurkowski | last post by:
Why is it so slow? I really like that queries but using DataReader i have done my task's much more faster than ising LINQ...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
2
by: shapper | last post by:
Hello, I have the following Linq query: List<PostsTaginsert = (from t in (from t in database.Tags join p in paper.Tags on t.Name equals p.Name select t).ToList() select new PostsTag {
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.