My problem is in my dbo.MyQueues
if you can see my ServiceLetter
data is
showing number instead of showing letters.
My dbo.Services
database looks like this.
ServiceId - ServicesName - ServiceLetter
1 - Admin Concerns - A
2 - Engineering - B
3 - Payments - C
public class Service
{
public int ServiceId { get; set; }
public string ServiceName { get; set; }
public string ServiceLetter { get; set; }
}
My dbo.MyQueues
database looks like this.
MyQueueId - Name - QueueNumber - ServiceId - ServiceLetter
1 - John - 1001 - 1 - 1
2 - Doe - 1002 - 2 - 2
3 - Jack - 1003 - 3 - 3
public class MyQueue
{
public int MyQueueId { get; set; }
public string Name { get; set; }
public string QueueNumber
{
get
{
return string.Format("{0}{1:000}", ServiceLetter ?? "?", MyQueueId);
}
set { }
}
public static Queue<MyQueue> todayQueue = new Queue<MyQueue>();
public int ServiceId { get; set; }
public string ServiceLetter { get; set; }
public virtual Service Service { get; set; }
}
I want my dbo.MyQueues
data to look like this.
MyQueueId - Name - QueueNumber - ServiceId - ServiceLetter
1 - John - A001 - 1 - A
2 - Doe - B002 - 2 - B
3 - Jack - C003 - 3 - C
my QueueNumber
is combination of my ServiceLetter
+ MyQueueId
thats why I want my Service letter to save as A letter not as A number so that I can have a QueueNumber data like A001
, B002
. In other words I need to make my ServiceLetter
on my dbo.MyQueue
to become letter right now its showing number.
Here is my Controller:
public class KiosksController : Controller
{
private DBContext db = new DBContext();
public ActionResult Index()
{
var services = db.Services;
return View(services.ToList());
}
[HttpGet]
public ActionResult GetQueueInfo(int? id, string ServiceLetter)
{
ViewBag.ServiceLetter = new SelectList(db.Services, "ServiceId", "ServiceLetter", id, ServiceLetter);
ViewBag.ServiceId = new SelectList(db.Services, "ServiceId", "ServiceName", id);
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SetQueueInfo([Bind(Include = "QueueId,Name,QueueNumber,ServiceId,ServiceLetter")] MyQueue queue/*, int? id,string Name, string QueueNumber, string ServiceLetter, int ServiceId*/, int? id)
{
if (ModelState.IsValid)
{
MyQueue.todayQueue.Enqueue(queue);
MyQueue.todayQueue.Count();
db.Queues.Add(queue);
db.SaveChanges();
return View(queue);
}
return View();
}
}
The scenario here is this.
in my index()
there is button of my services that when click will go to my GetQueueInfo
then in my GetQueueInfo
's view you enter your name then click confirm button to go to SetQueueInfo
and generate A queuenumber.
queuenumber is combination of ServiceLetter
+ MyQueueId
as of now Its generating me that but instead of letter on the beggining its showing me number like 1001
instead of A001
. badly need help here, i'm stuck.
A structure like that with your QueueNumber isn't a good idea relying on the possible presence of another entity. In EF6, and possibly future versions of EF Core this may work thanks to lazy loading, but even then it's not ideal because lazy loading will lead to performance issues.
Suggestion 1: If ServiceLetter is unique and important, and Service acts like a lookup table, and you want to use the ServiceLetter as part of another key; Use ServiceLetter as the PK.
public class Service
{
[Key]
public char ServiceLetter { get; set; }
public string ServiceName { get; set; }
}
public class MyQueue
{
public int MyQueueId { get; set; }
public string Name { get; set; }
[ForeignKey("Service")]
public char? ServiceLetter { get; set; }
public virtual Service Service { get; set; }
[NotMapped]
public string QueueNumber
{
get
{
return string.Format("{0}{1:000}", ServiceLetter.HasValue ? ServiceLetter.Value : "?", MyQueueId);
}
}
}
In this way the service letter is known by the queue and can be accessed while maintaining integrity as the FK to the service.
Suggestion 2: If your application is the sole custodian of the data (no other system/process modifies data) then adopt a DDD pattern to manage references to the Service so that the Queue can reliably track and persist a QueueNumber.
public class Service
{
[Key]
public int ServiceId { get; set; }
public char ServiceLetter { get; set; }
public string ServiceName { get; set; }
}
public class MyQueue
{
public int MyQueueId { get; internal set; }
public string Name { get; internal set; }
public virtual Service Service { get; internal set; }
// Persisted to the MyQueue table.
public string QueueNumber { get; internal set; }
public void UpdateService(Service service)
{
Service = service;
QueueNumber = string.Format("{0}{1:000}", service?.ServiceLetter ?? "?", MyQueueId);
}
}
The DDD approach restricts updates to methods whereby the method to update a service can compute the Queue Number. If it is possible to update a service letter on an existing service you would need logic to trigger the UpdateService on all Queue entries that reference that specific service.
If other systems can update the service reference or the service letter then you can utilize a health check process running on the database to search for Queue numbers that do not reflect their service reference and update those affected queue numbers. Normally I structure these to insert into an audit table where incorrect data is found followed by the update to the data to correct the inconsistency.