简体   繁体   中英

Entity Framework 6.1 to attach object based on unique constraint other than the primary key

I'm currently working on developing a model to comunicate with the jet.com API, and i got across a hardship with entity framework which could be very common while developing a EF data model to get data from an API.

So jet.com API is returning unique keys in most cases, but i don't want to use their keys as the primary keys in my database for a few different reasons, but i do have the abillaty in entity framework to implement unique constraints, now the question is if there's any way to implement some kind of override on the attetch method to attech the entity based on a unique key when the primary key is not set or not found?

I'd assume that i can probably write an extension method to handle this, but i just don't want to re-invent the wheel...

I was thinking about a possibility to implement some kind of exception handler for a unique key violation, but so far i didn't figure it out totally...

I'm not sure if this is what you were looking for, but you can map your primary keys in EF6+ through code:

modelBuilder.Entity<OfficeAssignment>().HasKey(t => t.InstructorID);

https://msdn.microsoft.com/en-us/data/jj591617.aspx#1.1

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