You can set the BaseCard identifier itself, but first you must use Fluent Api to indicate this and one-way - a lot of relationships. In addition, you do not need to specify attributes in your model classes. Your model classes will be as follows:
public class BaseCard { public int Id {get ; set; } public virtual ICollection<Skill> Skills { get; set; } } public class Skill { public int Id { get; set; } public int BaseCardId { get; set; } public virtual BaseCard BaseCard { get; set; } }
As you can see, I am changing the navigation properties as virtual. If you define your navigation property as virtual EF at run time, create a new class (dynamic proxy) derived from your BaseCard class and use it instead (the same thing happens with Skill). This new dynamically created class contains the logic for loading the navigation property on first access. This function is called lazy loading. It allows the Entity Framework to avoid loading the entire tree of dependent objects that are not needed from the database. You can find more information about this topic in these posts: Why are the default navigation properties virtual in EF and Entity Framework 4.1 Virtual Properties .
Another change I'm using in your model is to use ICollection <> type instead of List <> in the Skills property. As you can see in this post, the interface is good practice in this case. It allows you to later specify the implementation you want (maybe List <>).
Now, back to your problem, in your Context class, you need to override the OnModelCreating method to specify the relationship and column no autogenerate for Id in the BaseCards table.
public class YourContext : DbContext { public DbSet<BaseCard> BaseCards { get; set; } public DbSet<Skill> Skill { get; set; }
With this configuration, you should always set BaseCard object identifiers with a different value.
If you prefer to use data annotations, you can specify the same thing:
public class BaseCard { [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public int Id { get; set; } public virtual ICollection<Skill> Skills { get; set; } } public class Skill { [Key] public int Id { get; set; } public int BaseCardId { get; set; } [ForeignKey("BaseCardId")] public virtual BaseCard BaseCard { get; set; } }
My recommendation uses the Fluent API, it is more flexible, and you do not need to touch your model classes. In this post you can see useful tips.