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?
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]
コメントを追加