简体   繁体   中英

EF Code-First How To Read A From Table With A Composite Key

I have created a SQL Server table using EntityFramework 6 Code First that has a composite key: Inspector ID and Jurisdiction ID. Each combination of those fields should be unique on the table hence the composite key.

Class/Table Definition:

public class InspectorJurisdiction
    {
        [Key]
        [Column(Order = 1)]
        public int inspectorId { get; set; }

        [Key]
        [Column(Order = 2)]
        public int jurisId { get; set; }
    }

Web Context:

public DbSet<InspectorJurisdiction> InspectorJurisdictions { get; set; }

I am using the Repository design pattern to interface with the created table which has a Repository class/interface and web Context.

How do I wire up the

var record = context.InspectorJurisdictions.Find(????) 

statement to use the two fields that make up the composite key to read from the database table?

You should not create entity for many to many mapping. Instead each entity Inspector and Jurisdiction should contain collection of other objects.

For more information how to set it properly check documentation https://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM