Format:
Recent snippets matching tags of CodeFirst
var x = _context.Database.SqlQuery<Company>("SELECT * FROM Companies").ToList();
216 Views
no comments
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;
940 Views
no comments
public class GenderConfiguration : ComplexTypeConfiguration<TypeSex> { public GenereConfiguration() { // set column name with Fluent Api Property(g => g.Value).HasColumnName("Sex"); } }
128 Views
no comments
// enum type public enum Sex { Male=1, Female=2 } // wrapper class for enum support in EF4 public class TypeSex {
293 Views
no comments
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);
238 Views
no comments
//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>());
120 Views
no comments
DbDatabase.SetInitializer<TestContextEF>(null)
152 Views
no comments
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>;
146 Views
no comments
//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);
170 Views
no comments
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search
