LINQ Classes in Separate Files

M

Marc S

Instead of having everything in a single file that represents my
database I have a file for each table and a relationships.cs file for
defining my relationships. I then created a ClassExtenstions.cs file
with partial classes which provide custom functionality such as
getting the text description of a reference table row:

public partial class BillingTypeReferenceDataContext :
System.Data.Linq.DataContext
{
/// <summary>
/// Get the text description of the reference item for the ID
specified.
/// </summary>
/// <param name="pId">The ID of the record to evaluate.</param>
/// <returns>The Description if found, and empty string otherwise.</
returns>
public string GetDescription(int pId)
{
var qry = from tbl in this.BillingTypeReferences
where tbl.BillingTypeReferenceId == pId
select tbl.BillingTypeDescription;

List<string> values = qry.ToList();

if (values.Count > 0)
{
return values[0];
}
else
{
return "";
}

}


/// <summary>
/// Gets the ID of a reference item for the text description
specified.
/// </summary>
/// <param name="pDescription">The text description of the record to
evaluate.</param>
/// <returns>The ID of the record if found, -1 otherwise.</returns>
public short GetId(string pDescription)
{
var qry = from tbl in this.BillingTypeReferences
where tbl.BillingTypeDescription == pDescription
select tbl.BillingTypeReferenceId;

List<short> values = qry.ToList();

if (values.Count > 0)
{
return values[0];
}
else
{
return -1;
}

}
}





One problem that I ran into is trying to insert a record. Say I have a
LINQ class called Customers:


CustomersDataContext ctxt = new CustomersDataContext();
Customers cust = new Customers();
cust.x = x;
cust.y = y;
....

ctxt.Customers.InsertOnSubmit(cust);

This causes a null reference exception.

Am I doing something wrong with constructing my DB access through
LinqToSql?
 
M

Marc S

Aparently when you create your own relationships you need to create
instances of each EntitySet in the OnCreate() method.


Nothing to see here, move along...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top