EF5 Code First - Data Annotations vs Fluent API

marcusstarnes

I am new to Entity Framework and am about to embark on a new ASP.NET MVC project using EF5 Code First.

As I understand it, you can either use Data Annotations against the properties within your Domain Model objects, or you can use a Fluent API, to define the properties data type (in the DB when the objects are created), size, whether they are nullable and so on.

Is there a general recommended approach as to which to go with?

I couldn't see how when using Data Annotations, I can specify the datatype and size for that property when it's created in the DB, whereas with the Fluent API it looks like you can do the following:

Property(g => g.ModelName).HasColumnType("varchar");
Property(g => g.ModelName).HasMaxLength(100);
Property(g => g.ModelName).IsRequired();

When using Data Annotations, by default it appears to be using nvarchar(MAX) for all my string properties for example.

Is there a way to do the above using Data Annotations? Does it offer the same degree of 'tweaking' as Fluent API or should I be leaving the domain model alone and separate all of this 'tweaking' to separate Fluent API classes for each domain model object?

devdigital

I would recommend the fluent API. Data annotations are a subset, and don't for example include cascading delete for relationships.

Also, you may consider polluting your models with persistence logic such as database column names inappropriate.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Entity Framework Code First with Fluent API Concurrency `DbUpdateConcurrencyException` Not Raising

分類Dev

EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax

分類Dev

EF6 Code First and mapping via annotations, how do I build the configuration?

分類Dev

Create table and insert data into it during EF code first migration

分類Dev

Why does EF5 ChangeTracker code check classes that do not inherit from IAuditableTable?

分類Dev

Laravel Fluent vs Eloquent

分類Dev

Laravel Fluent vs Eloquent

分類Dev

Fluent API を使用した EF 6 Code First 関係。1 番目と 3 番目のテーブル間の関係を設定する方法、またはグループ化されたコレクションを取得する方法

分類Dev

EF6.1 optional-to-optional with fluent api mapping

分類Dev

MVC 5 - How to update relational table using EF code first

分類Dev

when the related data is included using fluent api, the list not populated in ajax (using Web Api)

分類Dev

EF Core 3.1.7 Data annotations for multiple 1:1 relationships in table

分類Dev

EF5およびテストユニットを使用したVS2012Expressでの列挙型の使用

分類Dev

How to get an unsaved file in VS Code to use the first line as the title?

分類Dev

Fluent Api Column order sytax

分類Dev

EF Core Fluent API(主キーからIDを削除する方法)

分類Dev

EF Code first 1 to 1 relationship error

分類Dev

EF Code First Navigation Property to same table

分類Dev

EF Code First Foreign Key Same Table

分類Dev

EF reverse engineering code first and stored procedures

分類Dev

Custom value type, EF Code First and routing

分類Dev

EF Code First, Model First or Db First? for a many changing medium-large app

分類Dev

EF5およびVS2015を使用してデータベースからモデルを更新できない

分類Dev

外部キーを含む複合主キーを構成する方法(EF Fluent API)

分類Dev

コレクション内のクラスインスタンスの取得-EFCode First Fluent API

分類Dev

VS Code extension Api to get the Range of the whole text of a document?

分類Dev

EntityFramework foreign key as primary key with fluent API

分類Dev

Scaling an Azure Elastic Pool with .NET Fluent API

分類Dev

Fluent API Configuration of One-To-Many relationship

Related 関連記事

  1. 1

    Entity Framework Code First with Fluent API Concurrency `DbUpdateConcurrencyException` Not Raising

  2. 2

    EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax

  3. 3

    EF6 Code First and mapping via annotations, how do I build the configuration?

  4. 4

    Create table and insert data into it during EF code first migration

  5. 5

    Why does EF5 ChangeTracker code check classes that do not inherit from IAuditableTable?

  6. 6

    Laravel Fluent vs Eloquent

  7. 7

    Laravel Fluent vs Eloquent

  8. 8

    Fluent API を使用した EF 6 Code First 関係。1 番目と 3 番目のテーブル間の関係を設定する方法、またはグループ化されたコレクションを取得する方法

  9. 9

    EF6.1 optional-to-optional with fluent api mapping

  10. 10

    MVC 5 - How to update relational table using EF code first

  11. 11

    when the related data is included using fluent api, the list not populated in ajax (using Web Api)

  12. 12

    EF Core 3.1.7 Data annotations for multiple 1:1 relationships in table

  13. 13

    EF5およびテストユニットを使用したVS2012Expressでの列挙型の使用

  14. 14

    How to get an unsaved file in VS Code to use the first line as the title?

  15. 15

    Fluent Api Column order sytax

  16. 16

    EF Core Fluent API(主キーからIDを削除する方法)

  17. 17

    EF Code first 1 to 1 relationship error

  18. 18

    EF Code First Navigation Property to same table

  19. 19

    EF Code First Foreign Key Same Table

  20. 20

    EF reverse engineering code first and stored procedures

  21. 21

    Custom value type, EF Code First and routing

  22. 22

    EF Code First, Model First or Db First? for a many changing medium-large app

  23. 23

    EF5およびVS2015を使用してデータベースからモデルを更新できない

  24. 24

    外部キーを含む複合主キーを構成する方法(EF Fluent API)

  25. 25

    コレクション内のクラスインスタンスの取得-EFCode First Fluent API

  26. 26

    VS Code extension Api to get the Range of the whole text of a document?

  27. 27

    EntityFramework foreign key as primary key with fluent API

  28. 28

    Scaling an Azure Elastic Pool with .NET Fluent API

  29. 29

    Fluent API Configuration of One-To-Many relationship

ホットタグ

アーカイブ