1

Using Entity Framework 6, I'm wondering if I need a Mapping file. I have a model defined like this:

[Table("UploadedFile")]
public partial class UploadedFile
{
    [Key, ForeignKey("Resource"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public System.Guid FileId { get; set; }
    public virtual Resource Resource { get; set; }

    //...
    public System.DateTime Modified { get; set; }

    public bool IsActive { get; set; }
    public byte[] RecordVersion { get; set; }

    public UploadedFile()
    {
        Resource = new Resource();
    }
} 

And my mapping file like this:

public class UploadedFileMapping : EntityTypeConfiguration<UploadedFile>
{
    public UploadedFileMapping()
    {
        //Primary key 
        HasKey(t => t.FileId);
        //Constraints 
        Property(t => t.RecordVersion).IsRowVersion();
    }
}

Can I just rely on attributes in the model? What are the pros/cons of using a mapping file?

I believe the [Key] attribute in the model negates the need for HasKey, is this correct?

M Kenyon II
  • 4,136
  • 4
  • 46
  • 94

2 Answers2

0

EF Fluent Api is better than data annotation because it permit domain model layer isolation and is more flexible (for example, IsRowVersion may added only in fluent Api). Key attribute is redundant.

Donald
  • 534
  • 1
  • 10
  • 18
  • However, it is a bit more of a pain. I wouldn't outright agree with Domain Model isolation... It depends how well you use DTOs/ViewModels/Models in your application. Also you should only really project out of the database what you really want, so there should never be a time that the entire database is modelled in code. ( this is solely for creating tables and migrations) – Callum Linington Oct 20 '15 at 10:58
0

You can just use attribute-based mapping. Mapping file is not required in your case. But I'd recommend to learn Fluent Api and use it.

Generally you have multiple options:

Don't use any explicit mappings - just rely on conventions (built-in or custom). It can be used in simple cases. To do so your DbContext should know about your entities via DbSet or through references on already known types.

  • Pros:
    • No additional code required.
  • Cons:
    • You have to know all conventions and this solution isn't flexible.

Use Data Annotations. Attribute-based mapping is very straighforward and simple.

  • Pros:
  • It's simple.
  • It can be used not only by Entity Framework but also for validation (e.g. in ASP.NET MVC or in WPF).
  • Some attributes can be defined only via Data Annotaions (e.g. MinValue and DefaultValue).
  • Cons:
    • It's not so flexible as Fluent Api.
    • It can be a bit difficult to read code of your model because of big amount of attributes.
    • Your model is tightly coupled with Entity Framework.

Use Fluent Api (have mapping files). It's the most advanced scenario with more possibilities (please take a look here for more detailed reference).

  • Pros:
    • It's flexible. You can have much more possibilities for mapping.
    • You can have clean domain model without Entity Framework dependency.
  • Cons:
    • It can be a bit difficult.

And also you can combine previous 3 approaches. You can use all these solutions together. For example:

  1. You are satisfied with default naming conventions so you don't have to define names for your tables.
  2. You can use Data Annotations to reuse them for validation.
  3. You can define not supported by Data Annotation parts using Fluent Api.

    • Cons:
    • It's a bit complicated to find out the entire configuration because it's defined in multiple places. So use this approach carefully.
Community
  • 1
  • 1