private static void startDb() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<SampleContext, Configuration>()); using (var ctx = new SampleContext()) { ctx.Database.Initialize(force: true); } }
protected void Page_Load(object sender, EventArgs e) { using (var ctx = new SampleContext()) { lblCategories.Text = ctx.Categories.Count().ToString(CultureInfo.InvariantCulture); } using (var ctx = new SampleContext()) { lblProducts.Text = ctx.Products.Count().ToString(CultureInfo.InvariantCulture); } }
public static async Task RunCommands() { using (var ctx = new SampleContext()) { var list = await ctx.Products.ToListAsync(); foreach (var product in list) { Console.WriteLine(product.Name); } var product1 = await ctx.Products.FirstOrDefaultAsync(product => product.Name.StartsWith("p")); if (product1 != null) { Console.WriteLine(product1.Name); } } }
public void TestExpand () { Func<SampleContext,List<string>> r1 = (ctx) => new List<string>() {"1","2","3"}; Func<SampleContext,List<string>> r2 = (ctx) => new List<string>() {"a","b","c","d"}; Func<SampleContext,List<string>> r3 = (ctx) => new List<string>() {"Green","Blue"}; var context = new SampleContext (); var parameterList = new ParameterList<SampleContext> (); parameterList.Add (new mtgfool.Utils.Parameter<SampleContext> ("number",r1)); parameterList.Add (new mtgfool.Utils.Parameter<SampleContext> ("letter",r2)); parameterList.Add (new mtgfool.Utils.Parameter<SampleContext> ("color",r3)); var x = parameterList.Expand (context); Assert.AreEqual (24, x.Count); var y = x.FindAll ((d) => d ["number"] == "2" && d ["letter"] == "d" && d ["color"] == "Green"); Assert.AreEqual (1, y.Count); }
static void Main(string[] args) { var connectionString = ConfigurationManager.ConnectionStrings["Demo"].ConnectionString; using (var context = new SampleContext(connectionString)) { // IQUERYABLE REPOSITORY //var repository = new QueryableRepository(context); //var people = repository.Query<Person>(x => x.FirstName == "Jason").ToList(); // OR //var people = repository.Query<Person>().Where(x => x.FirstName == "Jason").ToList(); // WHY NOT JUST USE THE CONTEXT?? //var people = context.People.Where(x => x.FirstName == "Jason").ToList(); var repository = new SpecificationRepository(context); var specification = new GetByFirstName("Jason").And(new GetByLastName("Mitchell")); var people = repository.Find(specification); Console.WriteLine("Found {0} people", people.Count); Console.ReadLine(); } }
private static async Task DoWork(SampleContext inputContext, SampleContext outputContext, Stopwatch doworkWatch) { doworkWatch.Start(); using (inputContext) { using (outputContext) { using (PostService svc = new PostService(inputContext)) { svc.CreateNewPost(1, "sampleSubject", "sampleBody"); //svc.DeletPostWithId(2); //svc.CreateNewPost(1, "sampleSubject", "sampleBody"); var allPosts = svc.GetAllPost().ToList(); allPosts.Count.ShouldBe(2); var result = await inputContext.CompareTo(outputContext); result.AreEqual.ShouldBe(true, result.ToString()); } } } doworkWatch.Stop(); }
public PostService(SampleContext ctx) : base() { myContext = ctx; }