Issue with ORM Lite Composite Key Workaround - ormlite-servicestack

I am implementing the composite key workaround suggested here:
https://github.com/ServiceStack/ServiceStack.OrmLite/#limitations
but I'm getting the SQL error "Invalid column name 'ID'" when trying to SELECT using the composite key values. SQL Profiler shows that the SELECT does indeed include a column 'ID' so I understand the SQL error. I'm just not sure how to use the workaround without getting the error?
DTO:
public class EmploymentHistory {
[PrimaryKey] // Workaround - Composite Key => Unique Key
public string ID {
get {
return
this.EmployeeID + "|"
+
this.DepartmentID + "|"
+
this.TitleID + "|"
+
this.StartDate.ToString("yyyy-MM-dd");
}
}
public int EmployeeID { get; set; }
public int DepartmentID { get; set; }
public int TitleID { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
[Ignore]
public Department Department { get; set; }
[Ignore]
public Title Title { get; set; }
}
Request DTO:
[Route("/employmenthistory/{EmployeeID}/{DepartmentID}/{TitleID}/{StartDate}", "GET")]
public class SingleEmploymentHistoryRequest : IReturn<Employee> {
public int EmployeeID { get; set; }
public int DepartmentID { get; set; }
public int TitleID { get; set; }
public DateTime StartDate { get; set; }
}
Service method:
public object Get(SingleEmploymentHistoryRequest request) {
EmploymentHistory employmentHistory = Db.Select<EmploymentHistory>()
.Where(eh =>
eh.EmployeeID == request.EmployeeID
&&
eh.DepartmentID == request.DepartmentID
&&
eh.TitleID == request.TitleID
&&
eh.StartDate.Date == request.StartDate.Date).FirstOrDefault();
if (employmentHistory == null)
throw new WebServiceException("EmploymentHistory not found");
return employmentHistory;
}

You shouldn't need to specify a fake ID primary key for custom queries:
var employmentHistory = Db.Single<EmploymentHistory>(eh =>
eh.EmployeeID == request.EmployeeID &&
eh.DepartmentID == request.DepartmentID &&
eh.TitleID == request.TitleID &&
eh.StartDate >= startDate.Date &&
eh.StartDate < startDate.Date.AddDays(1)));

Related

How to Conditionally MapFrom in Automapper using ProjectTo

I want to create a query for EFCore 5 using projection from Automapper 10.1.1 that will conditionally include associations.
If I were to write this in a select statement, I would do the following:
_dbContext.Employees
.Select(x => new EmployeeDto()
{
Id = x.Id,
Contract = includeContract ? new ContractDto()
{
Id = x.Contract.Id
} : null
})
.FirstAsync();
I attempted it like so:
Models
//Source
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Contract ActiveContract { get; set; }
}
public class Contract
{
public int Id { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
public Employee Employee { get; set; }
public int EmployeeId { get; set; }
}
//Destination
public class EmployeeDto
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public ContractDto ActiveContract { get; set; }
}
public class ContractDto
{
public int Id { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
}
AutoMapper Profile
public class EmployeeProfile : Profile
{
public EmployeeProfile()
{
bool includeContract = false;
CreateMap<Employee, EmployeeDto>()
.ForMember(x => x.ActiveContract, opt => opt.MapFrom(x => includeContract ? x.ActiveContract : null));
}
}
Instead of null, I've tried default(Contract) and (Contract)null these produce the same result.
Also I have a mapping for Contracts, just a simple <Contract, ContractDto>.
Query
bool includeContract = someCondition;
var result = await _dbContext.Employees
.ProjectTo<EmployeeDto>(_mapper.ConfigurationProvider, new { includeContract })
.FirstAsync(x => x.id == id);
My expected result is, I will be returned a EmployeeDto with the Contract being null, unless the includeContract is set to true.
However if the includeContract is false, the following error is thrown:
System.InvalidOperationException: Expression 'NULL' in SQL tree does not have type mapping assigned
the query expression produced:
Compiling query expression:
'DbSet<Employee>()
.Select(dtoEmployee => new EmployeeDto{
Age = dtoEmployee.Age,
Id = dtoEmployee.Id,
Name = dtoEmployee.Name,
ActiveContract = null == null ? null : new ContractDto{
Id = null.Id,
StartDate = null.StartDate,
EndDate = null.EndDate
}
}
)
.First(x => x.Id == __id_0)'
I know that is achievable by explicitly defining the expression within a ConvertUsing, but would like to avoid writing out my whole DTOs if possible.
Referring to the documentation linked via #Lucian in their comment.
The solution was to the adjust the AutoMapper config to the following:
CreateMap<Employee, EmployeeDto>()
.ForMember(x => x.ActiveContract, opt => opt.ExplicitExpansion())
And to adjust the query to the following:
var result = await _dbContext.Employees
.ProjectTo<EmployeeDto>(_mapper.ConfigurationProvider,
null,
dest => dest.ActiveContract
)
.FirstAsync(x => x.id == id);

How can I initialize one model in another model's controller action?

I created a web-app in Asp.net MVC and it has an order action. I have these two models for Order
public class Order
{
public int Id { get; set; }
public DateTimeOffset OrderTime { get; set; }
[InverseProperty("Order")]
public ICollection<OrderDetail> OrderDetails { get; set; }
}
and for OrderDetail
public class OrderDetail
{
public int Id { get; set; }
public int OrderId { get; set; }
public ICollection<Order> Order { get; set; }
public int MenuId { get; set; }
public int RestaurantId { get; set; }
public Menu Menu { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
And I created tables for them.
Also I created a controller for Order. It contains Index and Details actions. Index acction shows the list of order and every order has its own Detail link which should contain information of Order and related OrderDetail
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Order order = db.Orders.Find(id);
if (order == null)
{
return HttpNotFound();
}
return View(order);
}
And the problem is that OrderDetails is null. Can you suggest me how I can initialize OrderDetail in Details action?
You have to tell EntityFramework which navigation properties you want to include.
Order order = db.Orders
.Where( o => o.Id == id )
.Include( o => o.OrderDetails )
.SingleOrDefault();
But you cannot use Find method any more

Merging multiple List<CustomType> when grouping in LINQ

I have this models:
public class AudienceInfo
{
public int Id { get; set; }
public string Departments { get; set; }
public List<CountOfDestination> CountOfDestinations { get; set; }
}
public sealed class CountOfDestination
{
public string DestinationName { get; set; }
public int? CountRoom { get; set; }
public int? CountOfFiles { get; set; }
}
And this table in DB.
public class AudienceInfo : IModelWithId
{
public int Id { get; set; }
....
public RoomPurpose RoomPurpose { get; set; }
public List<AudienceInfo_File> Files { get; set; }
}
After selecting the "Departments" (where condition), I get a list of data. Then, I GroupBy "RoomPurpose" and get
"CountRoom" for every row = DestinationName. This part work correctly.
Also, i need to get the CountOfFiles ... don't know how to do this
return dbAudInfo
.Where(x => x.RightOfPreferentialUse.Id == Id)
.Select(x => new AudienceInfo
{
Departments = x.RightOfPreferentialUse.Name,
Date = date1,
CountOfDestinations = dbAudInfo
.GroupBy(z => new { z.RoomPurpose })
.Select(y => new CountOfDestination
{
DestinationName = y.Key.RoomPurpose.Name,
CountRoom = y.Count(z => z.RightOfPreferentialUse.Id == Id),
CountOfFiles = ?????????????????????????
}).ToList()
}).FirstOrDefaultAsync();
How can i connect a list in LINQ query with GroupBy.
Also, i need to get the CountOfFiles ... don't know how to do this
Use:
CountOfFiles = y.ToList().First(a => a.Id == y.Id).Files.Count()
How can i connect a list in LINQ query with GroupBy.
Can you elaborate?

Join tables and get data from both of them

I have been trying to get data from the table I have joined to the main user table, the second table is to hold images. My current code posted below, only return the ImageID from the table when I want to be retrieving the ImagePath field, just to note this is a separate table as the user can add many images.
These are the models:
[Table("accountInfo")] // Table name
public class accountInfo
{
[Key]
public int AccountID { get; set; }
public int UserId { get; set; }
public int UserIdent { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public virtual ICollection<UserImages > UserImages { get; set; }
}
[Table("UserImages")] // Table name
public class UserImages
{
[Key]
public int ImageID { get; set; }
public int AccountID { get; set; }
public string ImagePath { get; set; }
public string ImageDesc { get; set; }
public int ProfileImage { get; set; }
}
Controller:
public ActionResult Index()
{
int id = (int)WebSecurity.CurrentUserId;
var users = db.AccountInformation.Include(c => c.UserImages).Where(c => c.UserId == id);
return View(users.ToList());
}
I am assuming I have gone wrong in the models set up. Can anyone help?
var a = db.AccountInformation.Include(c => c.UserImages.Select(x => x.AccountId)).Where(c => c.UserId == id);

ASP.NET MVC 4 Code First Many to Many Adding to Collection

I am using ASP.NET MVC 4 code first pattern for database layer. I have a many to many relationship between UserProfile and Task. When I try to add a task to the the collection of tasks of a user, it's added but if I try to query it and see if it's there it's not showing up.
My model:
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string SirName { get; set; }
public string Position { get; set; }
public string Email { get; set; }
public ICollection<TaskModels> Tasks {get; set; }
public bool? isActive { get; set; }
public UserProfile()
{
Tasks = new HashSet<TaskModels>();
}
}
public class TaskModels
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public ICollection<UserProfile> Employees { get; set; }
public int TimeNeeded { get; set; }
public int TimeWorked { get; set; }
public string Status { get; set; }
public bool isActive { get; set; }
public TaskModels()
{
Employees = new HashSet<UserProfile>();
}
}
public class WorkLogModels
{
public int Id { get; set; }
public UserProfile Author { get; set; }
public DateTime TimeBeganWorking { get; set; }
public int TimeWorkedOn { get; set; }
public TaskModels Task { get; set; }
public string Description { get; set; }
}
public class TimeTrackerDb : DbContext
{
public TimeTrackerDb() : base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<TaskModels> Tasks { get; set; }
public DbSet<WorkLogModels> WorkLogs { get; set; }
}
I try to check if a UserProfile already exists in a Task's Employees list and it's always empty.
[HttpPost]
public ActionResult Create(WorkLogModels worklogmodels)
{
var tasks = db.Tasks.Where(x => x.Name == worklogmodels.Task.Name).SingleOrDefault();
if (tasks == null)
{
return View(worklogmodels);
}
if (ModelState.IsValid)
{
var user = db.UserProfiles.Where(x => x.UserId == WebSecurity.CurrentUserId).FirstOrDefault();
var task = db.Tasks.Where(x => x.Name == worklogmodels.Task.Name).FirstOrDefault();
WorkLogModels log = new WorkLogModels();
log.Description = worklogmodels.Description;
log.TimeBeganWorking = worklogmodels.TimeBeganWorking;
log.TimeWorkedOn = worklogmodels.TimeWorkedOn;
log.Author = user;
log.Task = task;
db.WorkLogs.Add(log);
if (!db.UserProfiles.Where(x => x.UserId == WebSecurity.CurrentUserId).First().Tasks.Any(x=> x.Name == worklogmodels.Task.Name))
{
db.UserProfiles.Where(x => x.UserId == WebSecurity.CurrentUserId).FirstOrDefault().Tasks.Add(task);
db.Tasks.Where(x => x.Name == worklogmodels.Task.Name).FirstOrDefault().Employees.Add(user);
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(worklogmodels);
}
I've been fighting with this for two days now.
Any help will be greatly appreciated
EDIT:
I am not sure if I made myself clear. In the Crate action for the WorkLog Controller I am trying to put the current user in the current task's collection and vice versa. It works correctly the first time, but then if I do it again it fails to skip the if statement and tries to add it once again and throws an exception : System.Data.SqlClient.SqlException. It's trying to add the same record to the intermediate table.

Resources