示例#1
0
 public Person GetPerson(int id)
 {
     using (var context = new PeopleCarsContext(_connectionString))
     {
         return(context.People.FirstOrDefault(p => p.Id == id));
     }
 }
示例#2
0
        public void AddQuestion(Question question, IEnumerable <string> tags)
        {
            using var ctx = new PeopleCarsContext(_connectionString);
            ctx.Questions.Add(question);
            ctx.SaveChanges();
            foreach (string tag in tags)
            {
                Tag t = GetTag(tag);
                int tagId;
                if (t == null)
                {
                    tagId = AddTag(tag);
                }
                else
                {
                    tagId = t.Id;
                }
                ctx.QuestionsTags.Add(new QuestionsTags
                {
                    QuestionId = question.Id,
                    TagId      = tagId
                });
            }

            ctx.SaveChanges();
        }
示例#3
0
 public IEnumerable <Person> GetPeople()
 {
     using (var context = new PeopleCarsContext(_connectionString))
     {
         return(context.People.Include(c => c.Cars).ToList());
     }
 }
示例#4
0
 public IEnumerable <Person> GetPeopleWithNoCars()
 {
     using (var context = new PeopleCarsContext(_connectionString))
     {
         return(context.People.Include(c => c.Cars).Where(p => p.Cars.Any(c => c.Make == "Lamborghini")).ToList());
     }
 }
示例#5
0
 private Tag GetTag(string name)
 {
     using (var ctx = new PeopleCarsContext(_connectionString))
     {
         return(ctx.Tags.FirstOrDefault(t => t.Name == name));
     }
 }
示例#6
0
 public void AddCar(Car car)
 {
     using (var context = new PeopleCarsContext(_connectionString))
     {
         context.Cars.Add(car);
         context.SaveChanges();
     }
 }
示例#7
0
 public List <Question> GetQuestionsForTag(string name)
 {
     using var ctx = new PeopleCarsContext(_connectionString);
     return(ctx.Questions.Include(q => q.QuestionsTags)
            .ThenInclude(qt => qt.Tag)
            .Where(c => c.QuestionsTags.Any(t => t.Tag.Name == name))
            .ToList());
 }
示例#8
0
 public List <Person> GetPeopleWithNoCars()
 {
     using var context = new PeopleCarsContext(_connectionString);
     return(context.People.Include(p => p.Cars)
            .Where(p => p.Cars.Count == 0).ToList());
     //return context.People.Include(c => c.Cars)
     //    .Where(p => p.Cars.Any(c => c.Make == "Lamborghini"))
     //    .ToList();
 }
示例#9
0
        private int AddTag(string name)
        {
            using var ctx = new PeopleCarsContext(_connectionString);
            var tag = new Tag {
                Name = name
            };

            ctx.Tags.Add(tag);
            ctx.SaveChanges();
            return(tag.Id);
        }
示例#10
0
 public List <Person> GetPeople()
 {
     using var context = new PeopleCarsContext(_connectionString);
     return(context.People.Include(c => c.Cars).ToList());
 }
示例#11
0
 public void AddPerson(Person person)
 {
     using var context = new PeopleCarsContext(_connectionString);
     context.People.Add(person);
     context.SaveChanges();
 }