Showing posts with label ef. Show all posts
Showing posts with label ef. Show all posts

Sunday, January 16, 2011

CTP5 [DataAnnotations]

DataAnnotations are sweet! 

The MSDN announcement to CTP5 has a listing of the Data Annotations provided.  Here they are formatted for your enjoyment:

  • Key
  • StringLength
  • MaxLength
  • ConcurrencyCheck
  • Required
  • Timestamp
  • ComplexType
  • Column    - Placed on a property to specify the column name, ordinal & data type
  • Table - Placed on a class to specify the table name and schema
  • InverseProperty - Placed on a navigation property to specify the property that represents the other end of a relationship
  • ForeignKey - Placed on a navigation property to specify the property that represents the foreign key of the relationship
  • DatabaseGenerated - Placed on a property to specify how the database generates a value for the property (Identity, Computed or None)
  • NotMapped - Placed on a property or class to exclude it from the database

Many-to-Many table mapping in EF CTP5

Was just reading that the method of many-to-many mapping is different in CTP5.  Here’s the method:

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Store> SoldAt { get; set; }
}

public class Store
{
public int Id { get; set; }
public string StoreName { get; set; }
public ICollection<Product> Products { get; set; }
}

public class MyContext : DbContext
{
public DbSet<Product> Products { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<Product>()
.HasMany(p => p.SoldAt)
.WithMany(s => s.Products)
.Map(mc => {
mc.ToTable("ProductsAtStores");
mc.MapLeftKey(p => p.Id, "ProductId");
mc.MapRightKey(s => s.Id, "StoreId");
});
}