Compound keys question

20 views
Skip to first unread message

David Ayala

unread,
Feb 11, 2018, 12:54:43 AM2/11/18
to sharpre...@googlegroups.com

Hi guys:

 

I’ve been using SharpRepository for the last year and a half, and before everything else, let me congratulate you guys for this fine piece of code.

 

I have a question about CompoundKey entities, is there a way to not use the RepositoryPrimaryKey

Attribute in my entity, and resolve by convetion.

 

I was reading your DefaultRepositoryConventions.GetPrimaryKeyName and the CompoundKeyRepositoryBase.GetPrimaryKeys,  and as far as I understand, the conventions method stops as soon as it finds the first property that meets the Id or EntityTypeName+ Id convention, and in the CompoundKeyRepositoryBase method, it only uses the RepositoryPrimaryKey attribute in order to find the primary key.

 

Would be possible to have a compound key convention kind of,  so that it allows me to have a “clean” and independent Business model. A Business model away for any kind of storage-like attributes.

 

I mean if I would like to distribute my model as nuget package, just as an example, I must add a reference to SharpRepository.Repository if I use the RepositoryPrimaryKey, therefore, my model would become coupled to your library.

 

I hope this question will make sense to you.

 

Best regards,

 

David

Omar Piani

unread,
Feb 12, 2018, 4:34:09 AM2/12/18
to SharpRepository
Answer is more or less no, depending the databases you are using behind.
For instance EF, that is the most common orm in c# does not support any convention for compound keys (https://docs.microsoft.com/en-us/ef/core/modeling/keys#conventions)

It will be great if we can add custom attributes at runtime but seems impossible https://stackoverflow.com/questions/129285/can-attributes-be-added-dynamically-in-c
and I think that RepositoryPrimaryKey can't extend dynamically a repository.

If a driver/orm uses custom data annotations/attributes you need to write it in your model.

For EF you can use fluent api.. and that's outside SharpRepository. https://docs.microsoft.com/en-us/ef/core/modeling/keys#fluent-api

Let me know if you find a more elegant solution.
Reply all
Reply to author
Forward
0 new messages