Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityFramework and other metadata #23

Closed
ryan-morris opened this issue Jul 25, 2018 · 2 comments
Closed

EntityFramework and other metadata #23

ryan-morris opened this issue Jul 25, 2018 · 2 comments

Comments

@ryan-morris
Copy link

ryan-morris commented Jul 25, 2018

The template works with EntityFramework because BaseEntity has an Id property that EF picks up by convention as a primary key. If you were to rename that to MyCustomPrimarykey rather than Id things start to fail at runtime with The entity type 'ToDoItem' requires a primary key to be defined..

The answer to this is usually to add a [Key] attribute to the primary key field but how would you go about doing that while still maintaining the "clean architecture" paradigm? I noticed there is a ToDoItemDTO in the Web project because of the [Required] attribute, but if you did another ToDoItemEntity you now are duplicating your model several times over if you need to annotate all of your properties for various reasons.

Is being more lax on "Duplicate Code" the best path or is there something I'm overlooking?

@camainc
Copy link

camainc commented Jul 25, 2018

You can use the fluent API to override the default key scheme:

https://docs.microsoft.com/en-us/ef/core/modeling/keys

https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/conventions/custom

@ardalis
Copy link
Owner

ardalis commented Jul 25, 2018

What @camainc said. I prefer to do EF mapping in my DbContext or entity-specific configuration classes in the Infrastructure project.

@ardalis ardalis closed this as completed Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants