Mixing Fluent API and DataAnnotations in EF code first

后端 未结 3 1611
北恋
北恋 2020-11-29 10:20

While we mostly use fluent configuration for our code-first POCOs, we have found it useful to use data annotations for things like the table name, PKs, etc. since it makes i

相关标签:
3条回答
  • 2020-11-29 11:04

    I personally haven't ran into any issues with mixing the code first fluent api and data annotations. I also wondered if there would be any crossover pain and I can honestly say I have yet to find any. Here's a few references to case studies on the subject to ease your mind.

    (Direct from the EF team) http://msdn.microsoft.com/en-us/data/jj591583.aspx

    (Part 1) http://www.codeproject.com/Articles/476966/FluentplusAPIplusvsplusDataplusAnnotations-plusWor

    0 讨论(0)
  • 2020-11-29 11:10

    I don't think it's a risk - as both things have equivalent counterparts for the most of it.

    But, personally, when I run into some sort of issues around structuring my entities - first thing I do is to remove annotations if any - and move all to fluent.

    Which over time led me to use pretty much straight fluent configuration (also freeing my my objects of any ties with the Db 'state of mind')...

    IMO it is 'safer' but only in a way that you can do more and control things exactly as you'd want them. Also helps with keeping things consistent and in one place.

    0 讨论(0)
  • 2020-11-29 11:11

    You can use Data Annotation attributes and Fluent API at the same time. Entity Framework gives precedence to Fluent API over Data Annotations attributes.

    0 讨论(0)
提交回复
热议问题