CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Format:
Recent snippets matching tags of CodeFirst
C#
var x = _context.Database.SqlQuery<Company>("SELECT * FROM Companies").ToList();
by biboyatienza   June 22, 2011 @ 2:19am
216 Views
no comments
 
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;
using System.Linq.Expressions;
using BusinessDomain.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
by biboyatienza   June 13, 2011 @ 9:22pm
940 Views
no comments
 
C#
public class GenderConfiguration : ComplexTypeConfiguration<TypeSex>
{
   public GenereConfiguration()
    {
      // set column name with Fluent Api
      Property(g => g.Value).HasColumnName("Sex");
    }
}
by pythonyan   April 26, 2011 @ 2:35am
Tags: EF4, CodeFirst
128 Views
no comments
 
C#
// enum type
public enum Sex
{
  Male=1,
  Female=2
}
 
// wrapper class for enum support in EF4
public class TypeSex
{
by pythonyan   April 26, 2011 @ 2:30am
Tags: EF4, CodeFirst
293 Views
no comments
 
C#
public class MyClassConfiguration :EntityTypeConfiguration<MyClass>
{
     public MyClassConfiguration() 
       {
         //define property key and database generated option, identity in this case, and table generated column order.  
         HasKey(p => p.Uid).Property(c => c.Uid).HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity).HasColumnOrder(1);
        // define column type (default navarchar(MAX) last release) and columnlength 
         Property(p => p.Name).HasColumnType("nvarchar(MAX)").HasMaxLength(4000);
       //Ignore mapping on specific property
       Ignore(p=>p.Description); 
by pythonyan   April 26, 2011 @ 2:14am
Tags: EF4, CodeFirst
238 Views
no comments
 
C#
//during context definition, alternatively choose.
//Drop and create database strategy
DbDatabase.SetInitializer(new DropCreateDatabaseAlways<TestContextEf>());
// Drop and create database if model changes
DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<TestContextEF>());
// Create database if not exists
DbDatabase.SetInitializer(new CreateDatabaseIfNotExists<TestContextEF>());
by pythonyan   April 26, 2011 @ 1:57am
Tags: EF4, CodeFirst
120 Views
no comments
 
C#
DbDatabase.SetInitializer<TestContextEF>(null)
by pythonyan   April 26, 2011 @ 1:50am
Tags: EF4, CodeFirst
152 Views
no comments
 
C#
public static class ContextHelpers
{
    public static TModel Create<TModel>(this DbContext context) where TModel : class
    {
        var dbsetProperty = (from property in context.GetType().GetProperties().ToList()
                        where typeof (IDbSet<TModel>).IsAssignableFrom(property.PropertyType)
                        select property).Single();
 
        var dbset = dbsetProperty.GetValue(context, null) as IDbSet<TModel>;
by John Nelson   March 17, 2011 @ 8:58am
146 Views
no comments
 
C#
//So here is what my site data context ends up looking like.
public class SiteDataContext : DbContext, ISiteDataContext
{
    public DbSet<User> Users { get; set; }
 
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().Property(u => u.Id)
            .HasColumnName("UserId");
        modelBuilder.Entity<User>().HasKey(u => u.Id);
by Mallioch   February 18, 2011 @ 8:25pm
170 Views
no comments
 
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate