public void SetUp() { _carId = "C1"; _inMemoryDemoContext = new InMemoryDemoContext(); _inMemoryDemoContext.Database.EnsureDeleted(); var driverId1 = "D1"; _inMemoryDemoContext.Cars.Add(new Car { CarId = _carId }); _inMemoryDemoContext.Drivers.Add(new Driver { DriverId = driverId1 }); _inMemoryDemoContext.CarDrivers.Add(new CarDriver { CarId = _carId, DriverId = driverId1 }); _inMemoryDemoContext.SaveChanges(); }
public List <Advertisement> GetAdvertisementsByCategory(Category category) { using (DemoContext context = new DemoContext()) { return((from adv in context.Advertisements .Include(a => a.SubCategory.Category) .Include(a => a.City.Province.Country) .Include(a => a.User) .Include(a => a.Images) .Include(a => a.Status) .Include(a => a.Type) where adv.SubCategory.Category.Id == category.Id select adv).ToList()); } }
public Expression <Func <DemoContext, Movie> > AddMovie(DemoContext db, AddMovieArgs args) { var movie = new Movie { Genre = args.Genre, Name = args.Name, Released = args.Released, Rating = args.Rating, }; db.Movies.Add(movie); db.SaveChanges(); return(ctx => ctx.Movies.First(m => m.Id == movie.Id)); }
private void GetData() { int.TryParse(this.cur_page_txt.Text, out curPage); page.CurPage = curPage; using (DemoContext context = new DemoContext()) { var listAll = context.order.Join(context.customer, a => a.customer.Id, b => b.Id, (a, b) => new { 姓名 = b.Name, 商品类型 = a.Type, 插入时间 = a.InsertTime }); page.RecordCount = listAll.ToList() == null ? 0 : listAll.ToList().Count; //总条数 page.PageCount = page.RecordCount / page.PageSize; //总页数 var customer_order_list = listAll.OrderByDescending(a => a.插入时间).Skip(page.PageSize * (page.CurPage - 1)).Take(page.PageSize).ToList(); this.label_count.Text = "共" + page.RecordCount.ToString() + "条 共" + page.PageCount.ToString() + "页"; this.customer_order_datagridview.DataSource = customer_order_list; } }
/// <summary> /// 查询数据 /// </summary> /// <param name="args"></param> static void Main1(string[] args) { using var context = new DemoContext(); //var italy = "Italy"; //var leagues = context.Leagues // .Where(x => x.Country == italy)// 如果这里的使用字面值,那么生成的 SQL 语句里也是写死的常量 // .ToList(); //var league1s = context.Leagues // .Where(x => // EF.Functions.Like(x.Country,"%e%"))// 如果这里的使用字面值,那么生成的 SQL 语句里也是写死的常量 // .ToList(); //foreach (var league in league1s) { // Console.WriteLine(league.Name); //} //var leagu2e = (from lg in context.Leagues // where lg.Country == "Italy" // select lg).ToList(); var first = context.Leagues.SingleOrDefault(x => x.Id == 2); var one = context.Leagues.Find(2); Console.WriteLine(first?.Name); Console.WriteLine(one?.Name); /* * info: Microsoft.EntityFrameworkCore.Database.Command[20101] * Executed DbCommand (29ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] * SELECT TOP(2) [l].[Id], [l].[Country], [l].[Name] * FROM [Leagues] AS [l] * WHERE [l].[Id] = 2 * Serie B * Serie B * 输出控制台中只有一条 SQL 语句,即 SingleOrDefault 查询命令的结果会缓存在 context 对象中 * 当我们使用 Find 继续查询时会先去 context 对象的缓存中查看是否有结果数据,如果没有再去数据库中查询 * 但是如果反过来,即先执行 Find 命令再执行 SingleOrDefault 命令则会输出两条 SQL 语句 */ var last = context.Leagues .OrderByDescending(x => x.Id) // 使用 LastOrDefault 方法之前需要进行排序,否则会报错 .LastOrDefault(x => x.Name.Contains("a")); Console.WriteLine(last?.Name); }
public List <Advertisement> GetLatestAdvertisement(int count) { using (DemoContext con = new DemoContext()) { return((from c in con.Advertisements .Include(a => a.SubCategory.Category) .Include(a => a.City.Province.Country) .Include(a => a.User) .Include(a => a.Images) .Include(a => a.Status) .Include(a => a.Type) orderby c.PostedOn select c ).Take(count).ToList()); } }
public List <Advertisement> GetAdvertisement() { using (DemoContext con = new DemoContext()) { return((from c in con.Advertisements .Include("SubCategroy.Category") .Include("City.Province.Country") .Include("User") .Include("Images") .Include("Type") .Include("Status") select c ).ToList()); } }
public ActionResult Form() { var user = Session["user"] as User; if (user == null) { ViewBag.ErrorMessage = "error while getting user by sesssion!"; return(View("Form")); } using (var ctx = new DemoContext()) { var userForms = ctx.Users.Include(x => x.Forms).FirstOrDefault(x => x.Id == user.Id).Forms; return(View(userForms)); } }
public ActionResult DetailProduct(int id) { DemoContext db = new DemoContext(); //var product= db.Advertisements.Include("AdvertisementImages").Where(x => x.ID == id).FirstOrDefault(); Advertisement objAdvertisement = new AdvertisementHandler().GetDetailAdvertisement(id); // AdvSumModel tem = new AdvSumModel(); //tem.Id = objAdvertisement.ID; //tem.ImgUrl = (objAdvertisement.Images.Count > 0) ? objAdvertisement.Images.First().Url : "/Images/temp/nophoto.png"; //Function for getting the Recommended AdvertisementProduct ViewBag.recomendedAdvertisement = new AdvertisementHandler().GetRecomendAdvertisement(objAdvertisement.SubCatagory.Catagory.ID).ToAdvertiseSumModel(); return(View(objAdvertisement)); }
public void InsertUsers(DemoContext context) { var users = new List <User> { new User() { Id = 1, Name = "User 1" }, new User() { Id = 2, Name = "User 2" }, }; context.Users.AddRange(users); }
private static Curso EditNoTrackingCurso(Curso curso, DemoContext ctx) { try { Curso existing = ctx.Set <Curso>().Find(curso.Id); if (existing == null) { return(null); } ctx.Entry(existing).CurrentValues.SetValues(curso); return(existing); } catch (Exception e) { return(null); } }
public IEnumerable <UserModel> GetUsers() { using (var db = new DemoContext()) { return(db .Users .Select(x => new UserModel() { Id = x.Id, Email = x.Email, FullName = x.FullName }) .ToList()); } }
private bool ValidateUnique(string description) { using (var context = new DemoContext()) { var tag = context.Tags.FirstOrDefault(t => t.Description == description); if ((tag != null) && (tag.Description == description)) { return(false); } else { return(true); } } }
protected void Application_Start() { System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion <DemoContext, Demo.DataAccessLayer.Migrations.Configuration>()); using (var context = new DemoContext()) { context.Database.Initialize(false); } AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); }
public void AssignTagtoTask(int taskid, int tagid) { using (var context = new DemoContext()) { TagEF tagtoadd = context.Tags.Find(tagid); TaskEF task = context.Tasks.Find(taskid); if ((tagtoadd != null) && (CountTags(taskid) < 10) && (tagtoadd.IsArchived == false)) { task.Tags.Add(tagtoadd); tagtoadd.Tasks.Add(task); context.SaveChanges(); } } }
private int CountTags(int taskid) { using (var context = new DemoContext()) { var task = context.Tasks.Find(taskid); if (task != null) { return(task.Tags.Count(q => q.IsArchived == false)); } else { return(10); } } }
//This Method received the data from the login View and Check in the data base , This usre is register or not in the database public JsonResult CheckValidUser(SiteUser model) { DemoContext _context = new DemoContext(); string result = "fial"; var DataItem = _context.tblSiteUser.Where(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password)).SingleOrDefault(); // This qUery Check in the database table weither the use is exist in the database or not if (DataItem != null) { Session["UserID"] = DataItem.ID.ToString(); Session["UserNamae"] = DataItem.UserName.ToString(); result = "Success"; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult AddSubCatagory(FormCollection data) { using (DemoContext _Context = new DemoContext()) { SubCatagory sCat = new SubCatagory(); sCat.Name = data["sCataName"]; sCat.Catagory = new Catagory { ID = Convert.ToInt32(data["catagoryList"]) }; _Context.Entry(sCat.Catagory).State = System.Data.Entity.EntityState.Unchanged; _Context.SubCatagories.Add(sCat); _Context.SaveChanges(); return(RedirectToAction("AddSubCatagory")); } }
public void InsertRoles(DemoContext context) { var roles = new List <Role> { new Role() { Id = 1, Name = "Role 1" }, new Role() { Id = 2, Name = "Role 2" }, }; context.Roles.AddRange(roles); }
void showDataGrid() { var dbContext = new DemoContext(); System.Collections.ObjectModel.ObservableCollection <DaraReg> valuesBd = new System.Collections.ObjectModel.ObservableCollection <DaraReg>(); var listBd = from p in dbContext.DataAll select p; foreach (var p in listBd) { valuesBd.Add(new DaraReg() { Id = p.Id, TargetKey = p.TargetKey, key = p.key }); } dataGrid1.ItemsSource = valuesBd; }
public int CountTask(int tagid) { using (var context = new DemoContext()) { var tag = context.Tags.Find(tagid); if (tag != null) { return(tag.Tasks.Count(q => q.IsArchived == false)); } else { Console.WriteLine("ERROR: Tag does not exist"); return(0); } } }
public ActionResult Index(UserLoginViewModel model) { // we logged in only by user name using (var ctx = new DemoContext()) { var user = ctx.Users.FirstOrDefault(x => x.Name == model.Name); if (user == null) { ViewBag.ErrorMessage = "come on write user name correctly!"; return(View("Index")); } Session["user"] = user; return(RedirectToAction("Form")); } }
internal static Entities.Person ToEntity(Person person, DemoContext demoContext) { if (person == null) { return(null); } else { return(new Entities.Person { Id = person.Id, FirstName = person.FirstName, LastName = person.LastName }); } }
public List <OptionDTO> GetAll() { List <OptionDTO> result = new List <OptionDTO>(); using (var context = new DemoContext()) { result = context.Options .Select(s => new OptionDTO { Text = s.Text, OptionId = s.OptionId }) .ToList(); } return(result); }
public List <QuestionTypeDTO> GetAll() { List <QuestionTypeDTO> result = new List <QuestionTypeDTO>(); using (var context = new DemoContext()) { result = context.QuestionTypes .Select(s => new QuestionTypeDTO { Description = s.Description, QuestionTypeId = s.QuestionTypeId }) .ToList(); } return(result); }
public List <Advertisement> GetFeaturedAd() { DemoContext context = new DemoContext(); using (context) { return((from c in context.Advertisements .Include(a => a.SubCategory.Category) .Include(a => a.City.Province.Country) .Include(a => a.User) .Include(a => a.Images) .Include(a => a.Status) .Include(a => a.Type) where c.isFeatured == true select c).ToList()); } }
public void DeleteTag(int tagid) { using (var context = new DemoContext()) { var tag = context.Tags.Find(tagid); if ((tag != null) && (CountTask(tagid) == 0)) { tag.IsArchived = true; context.SaveChanges(); } else { Console.WriteLine("ERROR: The the tag is assigned to a task"); } } }
public string AddEmployee(Employee employee) { if (employee != null) { using (DemoContext contextObj = new DemoContext()) { contextObj.employee.Add(employee); contextObj.SaveChanges(); return("Employee Added"); } } else { return("Invalid Record"); } }
public void BuildEmailTemplate(int regID) { DemoContext _Context = new DemoContext(); string body = System.IO.File.ReadAllText(HostingEnvironment.MapPath("~/EmailTemplate/") + "Text" + ".cshtml"); var regInfo = _Context.tblSiteUser.Where(x => x.ID == regID).FirstOrDefault(); //Now Create a Confirm link for send with Email //ControllNer /ViewName?Parameter var url = "http://*****:*****@ViewBag.ConfirmationLink", url); body = body.ToString(); BuildEmailTemplate("Your Account is Successfully Created", body, regInfo.Email); }
public ActionResult rejectPostStatus(int id) { DemoContext _context = new DemoContext(); Advertisement objAvd = _context.Advertisements.Include(x => x.City.Province.Country).Include(x => x.Images).Include(x => x.Status).Include(x => x.SubCatagory.Catagory).Include(x => x.Type).Include(x => x.User).Where(x => x.ID.Equals(id)).SingleOrDefault(); objAvd.Status = new AdvertisementStatus { ID = 3 }; _context.Entry(objAvd.Status).State = System.Data.Entity.EntityState.Unchanged; _context.Entry(objAvd).State = System.Data.Entity.EntityState.Modified; BuildEmailTemplate(objAvd.ID); _context.SaveChanges(); return(RedirectToAction("PenddingPost", new { area = "AdminSide", Controller = "Home" })); }
private void SeedContext(DemoContext context) { //seed some data in var demo = new DemoBaseClass() { NormalAttribute = "TEST" }; context.DemoBaseClasses.Add(demo); var demo1 = new DemoDerivedClass1() { NormalAttribute = "TEST1" }; context.DemoDerivedClassOnes.Add(demo1); var demo2 = new DemoDerivedClass2() { NormalAttribute = "TEST2" }; context.DemoDerivedClassTwos.Add(demo2); var demo3 = new DemoDerivedClass3() { NormalAttribute = "TEST3" }; context.DemoDerivedClassThrees.Add(demo3); context.SaveChanges(); }
protected ServiceBase(DemoContext context) { Context = context; }
public TeamService(DemoContext context) : base(context) { }
public void LinqTranslations_NormalAttribute_NormalOverrideBehaviour() { using (var connection = Effort.DbConnectionFactory.CreateTransient()) { using (var context = new DemoContext(connection)) { SeedContext(context); } using (var context = new DemoContext(connection)) { var result = context.DemoBaseClasses.First().NormalAttribute; Assert.AreEqual("TEST", result); var result1 = context.DemoDerivedClassOnes.First().NormalAttribute; Assert.AreEqual("Override 1", result1); var result2 = context.DemoDerivedClassTwos.First().NormalAttribute; Assert.AreEqual("Override 2", result2); var result3 = context.DemoDerivedClassThrees.First().NormalAttribute; Assert.AreEqual("Override 3", result3); } } }
public void LinqTranslations_CalculatedAttribute_OverrideBehaviour() { using (var connection = Effort.DbConnectionFactory.CreateTransient()) { using (var context = new DemoContext(connection)) { SeedContext(context); } using (var context = new DemoContext(connection)) { var qry = from m in context.DemoBaseClasses select m.CalculatedAttribute; Assert.AreEqual("TEST", qry.WithTranslations().First()); var qry1 = from m in context.DemoDerivedClassOnes select m.CalculatedAttribute; Assert.AreEqual("Calculated Override 1", qry1.WithTranslations().First()); var qry2 = from m in context.DemoDerivedClassTwos select m.CalculatedAttribute; Assert.AreEqual("Calculated Override 1", qry2.WithTranslations().First()); var qry3 = from m in context.DemoDerivedClassThrees select m.CalculatedAttribute; Assert.AreEqual("Override 3", qry3.WithTranslations().First()); } } }
public PeopleService(DemoContext context) : base (context) { }