Ben Foster
Ben Foster

Reputation: 34800

How to map properties in EF CTP5

In CTP 4 we could choose the properties we want to map like so:

    this.MapSingleType(i => new
{
    i.Id,
    i.OriginalFileName,
    i.Extension,
    i.MimeType,
    i.Width,
    i.Height,
    i.ImageStoreLocationId,
    i.AlternateText,
    i.ImageData
});

How do we achieve this in CTP5?

I tried using the following Map configuration but this does not appear to work since I still have to explicitly ignore (this.Ignore(..)) the properties I do not wish to map:

    Map(config =>
{
    config.Properties(i => new
    {
        i.OriginalFileName,
        i.Extension,
        i.MimeType,
        i.Width,
        i.Height,
        i.ImageStoreLocationId,
        i.AlternateText,
        i.ImageData
    });

    config.ToTable("Images");
});

Considering the new API is supposed to be more fluent, it's strange that I have to write more code to achieve the same thing.

Thanks Ben

Upvotes: 2

Views: 3809

Answers (2)

B Z
B Z

Reputation: 9453

This blog post has ctp 5 mapping samples.

http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

Make a clr-nullable property required:

modelBuilder.Entity<Product>() 
    .Property(p => p.Name) 
    .IsRequired();

Change string length:

modelBuilder.Entity<Product>() 
    .Property(p => p.Name) 
    .HasMaxLength(50);

Switch off Identity:

modelBuilder.Entity<Product>() 
    .Property(p => p.ProductId) 
    .HasDatabaseGenerationOption(DatabaseGenerationOption.None);

Ignore a property:

modelBuilder.Entity<Person>() 
    .Ignore(p => p.Name); 

Table & Column Mapping Change column name:

modelBuilder.Entity<Category>() 
    .Property(c => c.Name) 
    .HasColumnName("cat_name");

Change table name:

modelBuilder.Entity<Category>() 
    .ToTable("MyCategories");

Change table name with schema:

modelBuilder.Entity<Category>() 
    .ToTable("MyCategories", "sales");

Upvotes: 5

Morteza Manavi
Morteza Manavi

Reputation: 33206

CTP5 is indeed more powerful and flexible both in Data Annotations and fluent API. For example in CTP4 if we wanted to exclude a property from mapping we would have to explicitly map everything else with MapSingleType in order to skip the one we don't want, like the way you mentioned.
In CTP5 this can be done simply by using [NotMapped] attribute on the property or by this fluent API code:

this.Ignore(i => i.Id);

And you are done, no need to invoke Map method.

Upvotes: 1

Related Questions