contactmatt
contactmatt

Reputation: 18600

Linq 2 SQL - Use ID name of than 'Id'

I'm creating a database and I want to use a different naming convention other than 'Id' for the Id of the table. For example, I have a products table and I want to use ProductId as the primary key name. How do I do this?

When I'm generating my database from my POCO, i get the error 'Could not find key member 'Id' of key 'Id' on type 'myEntity'. The key may be wrong or the field or property on 'myEntity' has changed names.

Here's my entity:

public class Product
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert, Name="ProductId")]
    internal int ProductId{ get; set; }

    [Column (CanBeNull=false)]
    public string Name { get; set; }

    [Column (CanBeNull=false)]
    public DateTime CreatedDate { get; set; }

    [Column (CanBeNull=true)]
    public DateTime? ModifiedDate { get; set; }
}

Upvotes: 2

Views: 142

Answers (1)

Mark Cidade
Mark Cidade

Reputation: 99957

Is Products the only table referenced from your DataContext? This works for me:

static void Main(string[] args)
{           
   MyDatabase db = new MyDatabase(@"C:\Temp\Database.sdf");
   db.CreateDatabase();
}

public class MyDatabase : DataContext
{
    public Table<Product> Products;
    public MyDatabase(string connection) : base(connection) { }
}

[Table(Name="Products")]
public class Product
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert, Name="ProductId")]
    internal int ProductId{ get; set; }

    [Column (CanBeNull=false)]
    public string Name { get; set; }

    [Column (CanBeNull=false)]
    public DateTime CreatedDate { get; set; }

    [Column (CanBeNull=true)]
    public DateTime? ModifiedDate { get; set; }
}

Upvotes: 1

Related Questions