views:

254

answers:

2

I am having occasional trouble with my C# dbml where it starts not linking properly.

I do not know how to replicate the exact cause of the problem, it was working perfectly until I changed a database table and then deleted the table and readded it with the new schema. The error message I get is "The type or namespace name 'tbl' could not be found (are you missing a using directive or an assembly reference?)

The only fix I have found is to make a new dbml then re-add the tables and delete the old dbml. Once it starts breaking like this then every time I change the dbml, I need to repeat this fix very frustrating.

Any ideas what to do or why this is happening?

Microsoft Visual Studio 2008 Version 9.0.30729.1 SP Microsoft .NET Framework Version 3.5 SP1

A: 

You don't have to use a dbml in order to use L2S. You can create your own classes that describe your database tables and their relationships.

Below is an extract from Microsoft's Data Developer Center

You can certainly use LINQ to SQL without DBML files. All you need are classes decorated with the [Table] and [Column] attributes. For example, if you want to query a simple Customer table, define the following class:

[Table] public class Customer
{
  [Column(IsPrimaryKey=true)]  public int ID;
  [Column]                     public string Name;
}

Then you can start querying as follows:

var db = new DataContext();
var customers = db.GetTable<Customer>();
var query = customers.Where (c => c.Name.StartsWith ("a"));

You can even define assocation properties manually - the following creates classes for Customer and Purchase in a one:many relationship:

public class DemoDataContext : DataContext
{
  public DemoDataContext (string cxString) : base (cxString) { }
  public Table<Customer> Customers { get { return GetTable<Customer>(); } }
  public Table<Purchase> Purchases { get { return GetTable<Purchase>(); } }
}

[Table] public class Customer
{
  [Column(IsPrimaryKey=true)]  public int ID;
  [Column]                     public string Name;

  [Association (OtherKey="CustomerID")]
  public EntitySet<Purchase> Purchases = new EntitySet<Purchase>();
}

[Table] public class Purchase
{
  [Column(IsPrimaryKey=true)]  public int ID;
  [Column]                     public int CustomerID;
  [Column]                     public string Description;
  [Column]                     public decimal Price;
  [Column]                     public DateTime Date;

  EntityRef<Customer> custRef;

  [Association (Storage="custRef",ThisKey="CustomerID",IsForeignKey=true)]
  public Customer Customer
  {
    get { return custRef.Entity; } set { custRef.Entity = value; }
  }
}
Nicholas Murray
A: 

I found the answer here

http://stackoverflow.com/questions/2190977/designer-cs-is-deleted-on-adding-object-in-dbml

Philip
The solution is to move any using statements into the namespace brackets.
Philip