sashomaga

Fluent API -> Mapping -> Entity Framework

Aug 7th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using ConsoleBlog.Models;
  6.  
  7. namespace ConsoleBlog.DataAccess
  8. {
  9.     public class BlogContext : DbContext
  10.     {
  11.         //call base class with connection string name
  12.         public BlogContext()
  13.         : base("ForumDb")
  14.         {
  15.  
  16.         }
  17.  
  18.         //Fluent API
  19.         protected override void OnModelCreating(DbModelBuilder modelBuilder)
  20.         {
  21.             // class with configuratios for Tag table
  22.             modelBuilder.Configurations.Add(new TagMappings());
  23.  
  24.             modelBuilder.Entity<Post>().Property(x => x.Content).IsFixedLength().HasMaxLength(255);
  25.             base.OnModelCreating(modelBuilder);
  26.         }
  27.  
  28.         public DbSet<Category> Categories { get; set; }
  29.         public DbSet<Post> Posts { get; set; }
  30.         public DbSet<PostAnswer> PostAnswers { get; set; }
  31.         public DbSet<Tag> Tags { get; set; }
  32.     }
  33. }
  34.  
  35. namespace CodeFirst.DataAccess
  36. {
  37.     using System.Data.Entity.ModelConfiguration;
  38.  
  39.     using CodeFirst.Models;
  40.  
  41.     public class TagMappings : EntityTypeConfiguration<Tag>
  42.     {
  43.         public TagMappings()
  44.         {
  45.             this.HasKey(x => x.TagId);
  46.             this.Property(x => x.Text).IsUnicode(true);
  47.             this.Property(x => x.Text).HasMaxLength(255);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment