cool_spirit
cool_spirit

Reputation: 677

key entity in Entity Framework?

The problem for me is I have an existing datatable "T_store" in my database with 3 field: storeKey, StoreName, StoreDescription. and I now I want to create a MVC 3 app. to map to all this field, I have

public int Id {get; }{set;}
public int storeKey {get; }{set;}
public int StoreName {get; }{set;}
public int StoreDescription {get; }{set;}

but I got an error if I declare int id, since the existing datatable doesnt have this field ?

any ideas please

Upvotes: 0

Views: 74

Answers (2)

HichemSeeSharp
HichemSeeSharp

Reputation: 3318

I wonder why you need Id property as your table already has a key column but, I think this will resolve your problem :

[NotMapped]
public int Id { get; set; }
[Key]
public int storeKey { get; set; }
public int StoreName { get; set; }
public int StoreDescription { get; set; }

Upvotes: 1

bloparod
bloparod

Reputation: 1726

If you want the ID in the entity and in the table, create the ID column in the table. If you want ID only in the entity, use [NotMapped] for the ID property. Also, I guess that you will use storeKey as the key for your entity, so you should use [Key] on this property.

Upvotes: 2

Related Questions