开发者

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. H开发者_运维百科ow 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; }
}


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; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜