ASP.NET MVC: How can I put the StartDate of a current record on the EndDate of an earlier record
I have a class that makes appointments, the person making a appointments only inserts the start date and time, but the end date must be equal to the start date of the next appointments. My difficulty is in ensuring that the previous appointments always receives the EndDate as the StartDate of the current appointments
public class InfoAppointments : Entity
{
public bool Active { get; set; }
public bool Excluded { get; set; }
public string Status { get; set; }
public string Observation{ get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
EDIT
My Repository:
public class InfoAppointmentsRepository : Repository<InfoAppointments>, IAppointmentsRepository
{
public InfoAppointmentsRepository(RveContext rveContext) : base(rveContext)
{
}
public InfoAppointments FindByName(string name)
{
return Search(c => c.Name== name).FirstOrDefault();
}
public InfoAppointments FindByStatus()
{
throw new NotImplementedException();
}
public override void Remove(Guid id)
{
throw new NotImplementedException();
}
}
}
c# entity-framework
add a comment |
I have a class that makes appointments, the person making a appointments only inserts the start date and time, but the end date must be equal to the start date of the next appointments. My difficulty is in ensuring that the previous appointments always receives the EndDate as the StartDate of the current appointments
public class InfoAppointments : Entity
{
public bool Active { get; set; }
public bool Excluded { get; set; }
public string Status { get; set; }
public string Observation{ get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
EDIT
My Repository:
public class InfoAppointmentsRepository : Repository<InfoAppointments>, IAppointmentsRepository
{
public InfoAppointmentsRepository(RveContext rveContext) : base(rveContext)
{
}
public InfoAppointments FindByName(string name)
{
return Search(c => c.Name== name).FirstOrDefault();
}
public InfoAppointments FindByStatus()
{
throw new NotImplementedException();
}
public override void Remove(Guid id)
{
throw new NotImplementedException();
}
}
}
c# entity-framework
What is the problem then?.
– NicoRiff
Jan 2 at 13:38
when I make a appointment, the StartDate of this appointment must also be saved on the EndDate of the last appointments. Thats my problem =/
– Jhensen
Jan 2 at 13:46
FYI There is nothing in this question related to MVC or Visual Studio, so I removed those tags. It could be related to e.g. Entity Framework or SQL Server (or some other DBMS), if you now realize that those apply then please edit and clarify the question occordingly.
– Peter B
Jan 2 at 14:07
add a comment |
I have a class that makes appointments, the person making a appointments only inserts the start date and time, but the end date must be equal to the start date of the next appointments. My difficulty is in ensuring that the previous appointments always receives the EndDate as the StartDate of the current appointments
public class InfoAppointments : Entity
{
public bool Active { get; set; }
public bool Excluded { get; set; }
public string Status { get; set; }
public string Observation{ get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
EDIT
My Repository:
public class InfoAppointmentsRepository : Repository<InfoAppointments>, IAppointmentsRepository
{
public InfoAppointmentsRepository(RveContext rveContext) : base(rveContext)
{
}
public InfoAppointments FindByName(string name)
{
return Search(c => c.Name== name).FirstOrDefault();
}
public InfoAppointments FindByStatus()
{
throw new NotImplementedException();
}
public override void Remove(Guid id)
{
throw new NotImplementedException();
}
}
}
c# entity-framework
I have a class that makes appointments, the person making a appointments only inserts the start date and time, but the end date must be equal to the start date of the next appointments. My difficulty is in ensuring that the previous appointments always receives the EndDate as the StartDate of the current appointments
public class InfoAppointments : Entity
{
public bool Active { get; set; }
public bool Excluded { get; set; }
public string Status { get; set; }
public string Observation{ get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
EDIT
My Repository:
public class InfoAppointmentsRepository : Repository<InfoAppointments>, IAppointmentsRepository
{
public InfoAppointmentsRepository(RveContext rveContext) : base(rveContext)
{
}
public InfoAppointments FindByName(string name)
{
return Search(c => c.Name== name).FirstOrDefault();
}
public InfoAppointments FindByStatus()
{
throw new NotImplementedException();
}
public override void Remove(Guid id)
{
throw new NotImplementedException();
}
}
}
c# entity-framework
c# entity-framework
edited Jan 2 at 14:14
Jhensen
asked Jan 2 at 13:37
JhensenJhensen
125
125
What is the problem then?.
– NicoRiff
Jan 2 at 13:38
when I make a appointment, the StartDate of this appointment must also be saved on the EndDate of the last appointments. Thats my problem =/
– Jhensen
Jan 2 at 13:46
FYI There is nothing in this question related to MVC or Visual Studio, so I removed those tags. It could be related to e.g. Entity Framework or SQL Server (or some other DBMS), if you now realize that those apply then please edit and clarify the question occordingly.
– Peter B
Jan 2 at 14:07
add a comment |
What is the problem then?.
– NicoRiff
Jan 2 at 13:38
when I make a appointment, the StartDate of this appointment must also be saved on the EndDate of the last appointments. Thats my problem =/
– Jhensen
Jan 2 at 13:46
FYI There is nothing in this question related to MVC or Visual Studio, so I removed those tags. It could be related to e.g. Entity Framework or SQL Server (or some other DBMS), if you now realize that those apply then please edit and clarify the question occordingly.
– Peter B
Jan 2 at 14:07
What is the problem then?.
– NicoRiff
Jan 2 at 13:38
What is the problem then?.
– NicoRiff
Jan 2 at 13:38
when I make a appointment, the StartDate of this appointment must also be saved on the EndDate of the last appointments. Thats my problem =/
– Jhensen
Jan 2 at 13:46
when I make a appointment, the StartDate of this appointment must also be saved on the EndDate of the last appointments. Thats my problem =/
– Jhensen
Jan 2 at 13:46
FYI There is nothing in this question related to MVC or Visual Studio, so I removed those tags. It could be related to e.g. Entity Framework or SQL Server (or some other DBMS), if you now realize that those apply then please edit and clarify the question occordingly.
– Peter B
Jan 2 at 14:07
FYI There is nothing in this question related to MVC or Visual Studio, so I removed those tags. It could be related to e.g. Entity Framework or SQL Server (or some other DBMS), if you now realize that those apply then please edit and clarify the question occordingly.
– Peter B
Jan 2 at 14:07
add a comment |
1 Answer
1
active
oldest
votes
There are several possible solutions to this, and it may depend on your preference for adding this sort of business logic in your application code or in SQL (e.g. as a trigger). I would personally recommend the former, as this requirement may evolve over time, and may affect other components of your business logic.
I've made a few assumptions: 1) That you are using Entity Framework, and 2) Appointments don't overlap, and EndDate is unique. If that is the case, you could implement this functionality using similar logic to the following:
public class AppointmentService
{
private readonly MyContext _db;
public AppointmentService(MyContext db) => _db = db;
public void AddAppointment(InfoAppointments appointment)
{
// Update the previous appointment's end date
var previousAppointment = _db.Appointments
.OrderByDescending(e => e.EndDate)
.FirstOrDefault();
if (previousAppointment != null)
{
previousAppointment.EndDate = appointment.StartDate;
}
// Add the new appointment
_db.Appointments.Add(appointment);
_db.SaveChanges();
}
}
One other comment: based on your explanation, it appears that EndDate should default to null, but you've used a non-nullable type. I would change it to the following:
public DateTime? EndDate { get; set; }
Hi!, Yes im using Entity with repository pattern, forgot that information. Yes, the date should be nullable, thks for the advice. I have specific repository for each class and the generic, this is a logic that I should put in the specific then. Using a little DDD pattern can help at these hours hehe
– Jhensen
Jan 2 at 14:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007379%2fasp-net-mvc-how-can-i-put-the-startdate-of-a-current-record-on-the-enddate-of-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are several possible solutions to this, and it may depend on your preference for adding this sort of business logic in your application code or in SQL (e.g. as a trigger). I would personally recommend the former, as this requirement may evolve over time, and may affect other components of your business logic.
I've made a few assumptions: 1) That you are using Entity Framework, and 2) Appointments don't overlap, and EndDate is unique. If that is the case, you could implement this functionality using similar logic to the following:
public class AppointmentService
{
private readonly MyContext _db;
public AppointmentService(MyContext db) => _db = db;
public void AddAppointment(InfoAppointments appointment)
{
// Update the previous appointment's end date
var previousAppointment = _db.Appointments
.OrderByDescending(e => e.EndDate)
.FirstOrDefault();
if (previousAppointment != null)
{
previousAppointment.EndDate = appointment.StartDate;
}
// Add the new appointment
_db.Appointments.Add(appointment);
_db.SaveChanges();
}
}
One other comment: based on your explanation, it appears that EndDate should default to null, but you've used a non-nullable type. I would change it to the following:
public DateTime? EndDate { get; set; }
Hi!, Yes im using Entity with repository pattern, forgot that information. Yes, the date should be nullable, thks for the advice. I have specific repository for each class and the generic, this is a logic that I should put in the specific then. Using a little DDD pattern can help at these hours hehe
– Jhensen
Jan 2 at 14:01
add a comment |
There are several possible solutions to this, and it may depend on your preference for adding this sort of business logic in your application code or in SQL (e.g. as a trigger). I would personally recommend the former, as this requirement may evolve over time, and may affect other components of your business logic.
I've made a few assumptions: 1) That you are using Entity Framework, and 2) Appointments don't overlap, and EndDate is unique. If that is the case, you could implement this functionality using similar logic to the following:
public class AppointmentService
{
private readonly MyContext _db;
public AppointmentService(MyContext db) => _db = db;
public void AddAppointment(InfoAppointments appointment)
{
// Update the previous appointment's end date
var previousAppointment = _db.Appointments
.OrderByDescending(e => e.EndDate)
.FirstOrDefault();
if (previousAppointment != null)
{
previousAppointment.EndDate = appointment.StartDate;
}
// Add the new appointment
_db.Appointments.Add(appointment);
_db.SaveChanges();
}
}
One other comment: based on your explanation, it appears that EndDate should default to null, but you've used a non-nullable type. I would change it to the following:
public DateTime? EndDate { get; set; }
Hi!, Yes im using Entity with repository pattern, forgot that information. Yes, the date should be nullable, thks for the advice. I have specific repository for each class and the generic, this is a logic that I should put in the specific then. Using a little DDD pattern can help at these hours hehe
– Jhensen
Jan 2 at 14:01
add a comment |
There are several possible solutions to this, and it may depend on your preference for adding this sort of business logic in your application code or in SQL (e.g. as a trigger). I would personally recommend the former, as this requirement may evolve over time, and may affect other components of your business logic.
I've made a few assumptions: 1) That you are using Entity Framework, and 2) Appointments don't overlap, and EndDate is unique. If that is the case, you could implement this functionality using similar logic to the following:
public class AppointmentService
{
private readonly MyContext _db;
public AppointmentService(MyContext db) => _db = db;
public void AddAppointment(InfoAppointments appointment)
{
// Update the previous appointment's end date
var previousAppointment = _db.Appointments
.OrderByDescending(e => e.EndDate)
.FirstOrDefault();
if (previousAppointment != null)
{
previousAppointment.EndDate = appointment.StartDate;
}
// Add the new appointment
_db.Appointments.Add(appointment);
_db.SaveChanges();
}
}
One other comment: based on your explanation, it appears that EndDate should default to null, but you've used a non-nullable type. I would change it to the following:
public DateTime? EndDate { get; set; }
There are several possible solutions to this, and it may depend on your preference for adding this sort of business logic in your application code or in SQL (e.g. as a trigger). I would personally recommend the former, as this requirement may evolve over time, and may affect other components of your business logic.
I've made a few assumptions: 1) That you are using Entity Framework, and 2) Appointments don't overlap, and EndDate is unique. If that is the case, you could implement this functionality using similar logic to the following:
public class AppointmentService
{
private readonly MyContext _db;
public AppointmentService(MyContext db) => _db = db;
public void AddAppointment(InfoAppointments appointment)
{
// Update the previous appointment's end date
var previousAppointment = _db.Appointments
.OrderByDescending(e => e.EndDate)
.FirstOrDefault();
if (previousAppointment != null)
{
previousAppointment.EndDate = appointment.StartDate;
}
// Add the new appointment
_db.Appointments.Add(appointment);
_db.SaveChanges();
}
}
One other comment: based on your explanation, it appears that EndDate should default to null, but you've used a non-nullable type. I would change it to the following:
public DateTime? EndDate { get; set; }
answered Jan 2 at 13:50
Brett WolfingtonBrett Wolfington
5,31842545
5,31842545
Hi!, Yes im using Entity with repository pattern, forgot that information. Yes, the date should be nullable, thks for the advice. I have specific repository for each class and the generic, this is a logic that I should put in the specific then. Using a little DDD pattern can help at these hours hehe
– Jhensen
Jan 2 at 14:01
add a comment |
Hi!, Yes im using Entity with repository pattern, forgot that information. Yes, the date should be nullable, thks for the advice. I have specific repository for each class and the generic, this is a logic that I should put in the specific then. Using a little DDD pattern can help at these hours hehe
– Jhensen
Jan 2 at 14:01
Hi!, Yes im using Entity with repository pattern, forgot that information. Yes, the date should be nullable, thks for the advice. I have specific repository for each class and the generic, this is a logic that I should put in the specific then. Using a little DDD pattern can help at these hours hehe
– Jhensen
Jan 2 at 14:01
Hi!, Yes im using Entity with repository pattern, forgot that information. Yes, the date should be nullable, thks for the advice. I have specific repository for each class and the generic, this is a logic that I should put in the specific then. Using a little DDD pattern can help at these hours hehe
– Jhensen
Jan 2 at 14:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007379%2fasp-net-mvc-how-can-i-put-the-startdate-of-a-current-record-on-the-enddate-of-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the problem then?.
– NicoRiff
Jan 2 at 13:38
when I make a appointment, the StartDate of this appointment must also be saved on the EndDate of the last appointments. Thats my problem =/
– Jhensen
Jan 2 at 13:46
FYI There is nothing in this question related to MVC or Visual Studio, so I removed those tags. It could be related to e.g. Entity Framework or SQL Server (or some other DBMS), if you now realize that those apply then please edit and clarify the question occordingly.
– Peter B
Jan 2 at 14:07