ASP MVC Composite Key

Contoh composite key adalah tabel plan_account. Id unik plan_account adalah kombinasi plan_id dan account_id.

Capture

Jika tidak ditangani, composite key akan menghasilkan error pada ASP MVC.

Business.Entities.plan_account: : EntityType ‘plan_account’ has no key defined. Define the key for this EntityType.
plan_account: EntityType: EntitySet ‘plan_account’ is based on type ‘plan_account’ that has no keys defined.

Solusinya adalah dengan menambahkan baris di bawah pada file *.Context.cs

[code language=”csharp”]
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<plan_account>().HasKey(p => new { p.plan_id, p.account_id });
}
[/code]

Chandra Oemaryadi has written 244 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>