A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit.
public interface IEntity
{
int Id { get; set; }
}
public class Model : IEntity
{
public int Id { get; set; }
// ...
}
public static bool ExistById<T>(this DbSet<T> source, int id) where T : IEntity
{
return source.Count(c => c.Id == id) > 0;
}
public class User
{
public int Id {get;set;}
public string FirstName {get;set;}
public string SecondName {get;set;}
public DateTime DateOfBirth {get;set;}
public string Email {get;set;}
public string Password {get;set;}
public virtual Token Token { get; set; }
public int TokenId { get; set; }
}
public class College
{
public int? AccrLevel { get; set; }
public string Address { get; set; }
public virtual Area Area { get; set; }
public int AreaId { get; set; }
public bool? Demo { get; set; }
public virtual Director Director { get; set; }
public int DirectorId { get; set; }
public string Email { get; set; }
public bool? Full { get; set; }
[Key]
public int CollegeId { get; set; }
public string Locality { get; set; }
public virtual LocalityType LocalityType { get; set; }
public int LocalityTypeId { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Site { get; set; }
public virtual ICollection<Speciality> Specialities { get; set; }
public virtual Status Status { get; set; }
public int? StatusId { get; set; }
public virtual University University { get; set; }
public int? UniversityId { get; set; }
}
public class Speciality
{
public virtual ICollection<College> Colleges { get; set; }
[ForeignKey("DirectionCode")]
public virtual Direction Direction { get; set; }
public string DirectionCode { get; set; }
public string Name { get; set; }
[Key]
public string SpecialityCode { get; set; }
}
var list = new List<string>() { "one", "two", "three", "four" };
var expected = "two";
var result = list.Contains(expected);
// либо
result = list.Count(a => a == expected) > 0;
var regions = new List<string>() { "region 1", "region 2", "region 3", "region 4" };
repository.Colleges.Where(college => regions.Count(a => a == college.Area.Region.Name) > 0);
public class Area // Район
{
public int Id { get; set; }
public string Name { get; set; }
public int RegionId { get; set; }
public virtual Region Region {get; set;}
public byte[] Map { get; set; }
}
public class Region // Область
{
public int Id { get; set; }
public string Name { get; set; }
public byte[] Map { get; set; }
}
public class Area // Район
{
[Key]
public int AreaId { get; set; }
public string Name { get; set; }
public int RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual Region Region {get; set;}
public byte[] Map { get; set; }
}
public class Region // Область
{
[Key]
public int RegionId { get; set; }
public string Name { get; set; }
public byte[] Map { get; set; }
}