I am using Telerik Open Access for database operation. I have a Parent Class named Order and it includes 2 classes named OrderHistory and Tasks. Now any kind of event happen like if order is put on hold or put for dispatching it's related entry will be put on OrderHistory table and Order table will be updated. Now I have done following code but it works sometimes but sometimes not. I don't know much about this telerik open access and automapper.
controller call:
OrderDTO updateorder = orderManagement.GetOrdersByOrderId(2);
updateorder.QueueId = 3;
updateorder.IsLocked = false;
updateorder.UpdatedBy = Convert.ToInt32(Session["UserId"], CultureInfo.CurrentCulture);
updateorder.UpdatedDate = DateTime.Now;
OrderHistoryDTO alertDto = new OrderHistoryDTO()
{
Event = 'Putting On Hold',
OrderID = orderDTO.Id
UserID = Convert.ToInt32(Session["UserId"], CultureInfo.CurrentCulture),
OccuerdDate = DateTime.Now,
EventType = 'Event'
};
updateorder.OrderHistories.Clear();
updateorder.OrderHistories.Add(alertDto);
updateorder = orderManagement.UpdateOrder(updateorder);
db operations
public OrderDTO UpdateOrder(OrderDTO orderEntity)
{
AutoMapper.Mapper.CreateMap<OrderDTO, Order>()
.ForMember(d => d.Tasks, m => m.Ignore())
.ForMember(d => d.OrderHistories, m => m.MapFrom(s => s.OrderHistories));
AutoMapper.Mapper.CreateMap<OrderHistoryDTO, OrderHistory>();
var orderBase = AutoMapper.Mapper.Map<Order>(orderEntity); // It will sometimes make OrderHistories list count to 0. though in starting orderEntity has OrderHistories count = 1.
base.Update(orderBase);
base.Save();
orderEntity = AutoMapper.Mapper.Map<OrderDTO>(orderBase);
return orderEntity;
}
OrderDTO
public class OrderDTO
{
public int OrderID { get; set; }
public bool? IsLocked { get; set; }
public int QueueId { get; set; }
[ScriptIgnore]
private IList<OrderHistoryDTO> _orderHistories = new List<OrderHistoryDTO>();
[ScriptIgnore]
public virtual IList<OrderHistoryDTO> OrderHistories
{
get { return this._orderHistories; }
}
public DateTime? UpdatedDate { get; set; }
public int? UpdatedBy { get; set; }
}
OrderHistoryDTO
public class OrderHistoryDTO
{
public int ID { get; set; }
public int UserID { get; set; }
public int OrderID { get; set; }
public string Event { get; set; }
public string EventType { get; set; }
public DateTime? OccuerdDate { get; set; }
public UserDTO User { get; set; }
}
Related
my simple app has 10 tables, each of these tables has same propery "CreatedDate" which is a DateTime prop and as its name says its holding creation date.
Since I'm fetching all the data through DTO's for example if I want to get articles from database I'm mapping it to ArticleDto and returning data to the user.
And I'm doing that all for each 10 classes-entites.
And each Dto, (ArticleDto, GroupDto, UserDto, TownDto, AddressDto) all of them now have DateTime property which I'm populating when retrieving data from database..
Is it possible to achieve somehow that this prop is automatically populated ?
This is how I am doing it right now for Towns for example:
public class TownGetDto : DateTimeGetDto
{
public long Id { get; set; }
public string Title { get; set; }
public string ZipCode { get; set; }
public string DialingCode { get; set; }
public long CountryId { get; set; }
}
public class DateTimeGetDto
{
public DateTime CreatedDate { get; set; }
}
public async Task<TownGetDto> GetTownsAsync(CancellationToken cancellationToken)
{
var towns = await _dbContext.Towns
.Select(x => new TownGetDto
{
Id = x.Id,
Title = x.Title,
DialingCode = x.DialingCode,
ZipCode = x.ZipCode,
CountryId = x.CountryId,
CreatedDate = x.CreatedDate // How to get rid of this prop?
})
.ToListAsync(cancellationToken);
return towns;
}
As you can see I am popualting CreatedDate in Select and I'm doing that for all my classes/entities..
Is it possible somehow to fill-populate this prop automatically ?
Thanks everyone,
Cheers
You still need to specify to each of the 10 classes where the DateTime value should come from, but it might be cleaner to use constructors and inheritance
In DateTimeGetDto:
public DateTime CreatedDate { get; set; }
public DateTimeGetDto(DateTime dateTime)
{
CreatedDate = dateTime;
}
In TownGetDto:
public class TownGetDto : DateTimeGetDto
{
public long Id { get; set; }
public string Title { get; set; }
public string ZipCode { get; set; }
public string DialingCode { get; set; }
public long CountryId { get; set; }
public TownGetDto(Town town) : base(town.CreatedDate)
{
Id = town.Id;
Title = town.Title;
DialingCode = town.DialingCode;
ZipCode = town.ZipCode;
CountryId = town.CountryId;
CreatedDate = town.CreatedDate;
}
public async Task<TownGetDto> GetTownsAsync(CancellationToken cancellationToken)
{
var towns = await _dbContext.Towns
.Select(x => new TownGetDto(x))
.ToListAsync(cancellationToken);
return towns;
}
}
I have gone through numerous examples, trying to figure out how to automatically set the CreatedDate value for each entity when saving changes to through unitOfWork and Repository pattern.
So what i have is simple POCO:
public class Partners:IAutoGenerateDateFields
{
[Key]
public int PartnerId { get; set; }
public string PartnerCode { get; set; }
public string PartnerName { get; set; }
public string CompanyName { get; set; }
public string City { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string TaxId { get; set; }
public int PartnerTypeId { get; set; }
public int PartnerStateId { get; set; }
public int LocationId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreatedDate { get; set; }
// [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
// public DateTime ModifiedDate { get;set;}
public virtual Locations Location { get; set; }
}
This Class implements the IAutoGenerateDateFields interface:
interface IAutoGenerateDateFields
{
DateTime CreatedDate { get; set; }
// DateTime ModifiedDate { get; set; }
}
Finally in my unitOfWork class i check if the added/modified entity implements the above interface and set the CreatedDate value to current date:
public void Save() {
context.ChangeTracker.DetectChanges();
var added = context.ChangeTracker.Entries()
.Where(t => t.State == EntityState.Added)
.Select(t => t.Entity).ToArray();
foreach (var entity in added) {
if (entity is IAutoGenerateDateFields) {
var track = entity as IAutoGenerateDateFields;
track.CreatedDate = DateTime.Now;
}
}
var modified = context.ChangeTracker.Entries()
.Where(t => t.State == EntityState.Modified)
.Select(t => t.Entity).ToArray();
foreach (var entity in modified) {
if (entity is IAutoGenerateDateFields) {
var track = entity as IAutoGenerateDateFields;
track.CreatedDate = DateTime.Now;
}
}
context.SaveChanges();
}
But every time i hit savechanges i get an error that CreatedDate cannot be null. Thus, the entity does not get set with the current date.
What am i missing here?
Remove the [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute, since you generate the value in your code.
I basically did the same thing yesterday, and the following code is working fine :
var pendingChanges = GetContext().ChangeTracker.Entries<T>().Select(e => e.Entity).ToList();
foreach (var entity in pendingChanges)
{
entity.DateModified = DateTime.Now
}
I have the following error when running my get method for a list in my controller:
The specified LINQ expression contains references to queries that are associated with different contexts.
Having debugged the controller it is the orderby statement that produces the error
The Method is:
public ActionResult OwnerList()
{
var owners = (from s in db.Owners
orderby Peopledb.Posts.FirstOrDefault(x => x.PostId == s.PostId).PostName
select s).ToList();
var viewModel = owners.Select(t => new OwnerListViewModel
{
Created = t.Created,
PostName = Peopledb.Posts.FirstOrDefault(x => x.PostId == t.PostId).PostName,
Dormant = t.Dormant,
OwnerId = t.OwnerId,
});
return PartialView("_OwnerList", viewModel);
}
The Class for Owner in the first database is, dbcontext = IARContext:
public class Owner
{
public int OwnerId { get; set; }
[Column(TypeName = "int")]
public int PostId { get; set; }
[Column(TypeName = "bit")]
public bool Dormant { get; set; }
[Column(TypeName = "datetime2")]
public DateTime Created { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
public People.Models.Post Post { get; set; }
}
The Class for Post in the second database is, dbcontext = PeopleContext:
public class Post
{
public int PostId { get; set; }
[StringLength(50)]
[Column(TypeName = "nvarchar")]
public string PostName { get; set; }
[Column(TypeName = "bit")]
public bool Dormant { get; set; }
[StringLength(350)]
[Column(TypeName = "nvarchar")]
public string Description { get; set; }
public virtual ICollection<Contract> Contracts { get; set; }
public virtual ICollection<Owner> Owners { get; set; }
}
I am trying to look up PostName from Post in the People db when displaying a list of Owners from the IAR db
for me the message is quite clear: you are mixing two contexts. That is not possible. This would nearly mean creating an hadhoc dblinq.
The solutions are :
a global context
a separation by code (if the context can't be globalized):
for me you should have
public ActionResult OwnerList()
{
var owners = (from s in db.Owners
//can't order from here without a dbling/global context
select s);
//may be a where is missing here ?
List<DAOSomeName> viewModel = owners.Select(t => new DAOSomeName
{
Created = t.Created,
Dormant = t.Dormant,
OwnerId = t.OwnerId,
});// .ToList(); the materialization is done by the following foreach
//until here, no run to the db, no data transfered.
foreach (DAOSomeName m in viewModel ) {
m.PostName = Peopledb.Posts.Where(x => x.PostId == t.PostId).
Select(x => x.PostName).FirstOrDefault();
//this way you also handle the null case pointed by Trevor
}
//please note that this way, yout view model is not anymore linked
//to the context, except if one property is a navigation property
return PartialView("_OwnerList", viewModel.OrderBy(x => x.PostName));
}
public class DAOSomeName {
public DateTime Created {get; set;}
//Dormant, OwnerId, PostName...
}
By amending my controller to this:
public ActionResult OwnerList()
{
var posts = new List<People.Models.Post>(Peopledb.Posts);
var owners = new List<Owner>(db.Owners);
var ownerposts = (from c in posts
join d in owners on c.PostId equals d.PostId
orderby c.PostName
select new OwnerPost { OwnerId = d.OwnerId, PostName = c.PostName, Created = d.Created, Dormant = d.Dormant }).ToList();
var viewModel = ownerposts.Select(t => new OwnerListViewModel
{
Created = t.Created,
PostName = t.PostName,
Dormant = t.Dormant,
OwnerId = t.OwnerId,
});
return PartialView("_OwnerList", viewModel);
}
and adding a OwnerPost Class:
public class OwnerPost
{
public int OwnerId { get; set; }
public string PostName { get; set; }
public bool Dormant { get; set; }
public DateTime Created { get; set; }
}
I solved the issue
This a simple project where users can search for job postings by area of expertise. The relationship between Areas and Postings are Many-to-many. I seem to be able to get to the very last part of retrieving the correctly filtered list, but getting back into the view model keeps giving me different errors:
ViewModel:
public class AreaOfertasViewModel
{
public Oferta UnaOferta { get; set; }
public SelectList AreasTrabajo { get; set; }
public IEnumerable<Oferta> Ofertas { get; set; }
public int idArea { get; set; }
public AreaOfertasViewModel()
{
this.UnaOferta = UnaOferta;
this.Ofertas = new List<Oferta>();
cargarAreas();
}
private void cargarAreas()
{
PostulaOfertaContext db = new PostulaOfertaContext();
this.AreasTrabajo = new SelectList(db.Areas, "areaId", "Area");
}
}
}
Controller:
public ActionResult SearchXArea()
{
return View(new AreaOfertasViewModel());
}
[HttpPost]
public ActionResult SearchXArea(AreaOfertasViewModel aovm)
{
int id = aovm.idArea;
PostulaOfertaContext db = new PostulaOfertaContext();
var area = db.Areas.Where(c => c.areaId == id);
var ofertas = from c in db.Ofertas.Where(r => r.AreaTrabajo == area)
select c;
aovm.Ofertas = (IEnumerable<Oferta>)ofertas.ToList();
return View(aovm);
}
The line giving me issues is
aovm.Ofertas = (IEnumerable)ofertas.ToList();
I've tried List<> for Ofertas, and I've tried leaving it as .ToList() without casting, and casting it as different things, but it gives me errors about not being able to cast it, and "Cannot compare elements of type 'System.Collections.Generic.List`1'. Only primitive types, enumeration types and entity types are supported."
What's the solution here?
Model for AreaTrabajo:
public class AreaTrabajo
{
[Key]
public int areaId { get; set; }
public string Area { get; set; }
public virtual List<Oferta> oferta { get; set; }
}
Model for Oferta:
public class Oferta
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string Titulo { get; set; }
[Required]
public DateTime Vencimiento { get; set; }
[Required]
public string Cargo { get; set; }
[Required]
public int HorarioComienzo { get; set; }
[Required]
public int HorarioFin { get; set; }
[Required]
public string DescripcionTareas { get; set; }
public int Remuneracion { get; set; }
[Required]
public int RangoEdadMin { get; set; }
[Required]
public int RangoEdadMax { get; set; }
public string TipoFormacion { get; set; }
public string Idiomas { get; set; }
public string Competencias { get; set; }
public string OtrosEstudios { get; set; }
public string Estado { get; set; }
public virtual List<AreaTrabajo> AreaTrabajo { get; set; }
public virtual TipoContrato TipoContrato { get; set; }
public virtual Empresa Empresa { get; set; }
public virtual List<Postulante> Postulantes { get; set; }
}
Answer
[HttpPost]
public ActionResult SearchXArea(AreaOfertasViewModel aovm)
{
int id = aovm.idArea;
PostulaOfertaContext db = new PostulaOfertaContext();
var area = db.Areas.Where(c => c.areaId == id).FirstOrDefault();
var ofertas = db.Ofertas.Where(s => s.AreaTrabajo.All(e => e.areaId == area.areaId)).ToList();
aovm.Ofertas = ofertas;
return View(aovm);
}
Sorry if my question wasn't clear enough. I needed to filter out from the many-to-many relationship, and this solved it.
You are getting an error because the actual sql is executed when you call tolist(). The error is in your sql because you are comparing AreaTrabago to a list.
[HttpPost]
public ActionResult SearchXArea(AreaOfertasViewModel aovm)
{
int id = aovm.idArea;
PostulaOfertaContext db = new PostulaOfertaContext();
var area = db.Areas.Where(c => c.areaId == id).FirstOrDefault();
var ofertas = db.Ofertas.Where(s => s.AreaTrabajo.All(e => e.areaId == area.areaId)).ToList();
aovm.Ofertas = ofertas;
return View(aovm);
}
Sorry if my question wasn't clear enough. I couldn't get the many-to-many relationship, and this solved the filtering problem perfectly.
I have two API calls. GetExam and SaveExam. GetExam serializes to JSON which means by the time I go to save, the entity is detached. This isnt a problem, I can go retrieve the entity by its primary key and update its properties manually.
However, when I do so the exam questions get its current collection duplicated. For example, if examToSave.ExamQuestions had a few questions deleted, and a new one added all selectedExam.exam_question are duplicated and the new one is added in. Eg. if 3 questions existed, I deleted 1 and added 4 there will now be 7.
Domain models:
public partial class exam
{
public exam()
{
this.exam_question = new HashSet<exam_question>();
}
public int ID { get; set; }
public string ExamName { get; set; }
public string ExamDesc { get; set; }
public Nullable<decimal> TimeToComplete { get; set; }
public bool AllowBackStep { get; set; }
public bool RandomizeAnswerOrder { get; set; }
public int Attempts { get; set; }
public virtual ICollection<exam_question> exam_question { get; set; }
}
public partial class exam_question
{
public exam_question()
{
this.exam_answer = new HashSet<exam_answer>();
}
public int ID { get; set; }
public int ExamID { get; set; }
public string QuestionText { get; set; }
public bool IsFreeForm { get; set; }
public virtual exam exam { get; set; }
public virtual ICollection<exam_answer> exam_answer { get; set; }
}
public partial class exam_answer
{
public int ID { get; set; }
public string AnswerText { get; set; }
public int QuestionID { get; set; }
public bool IsCorrect { get; set; }
public virtual exam_question exam_question { get; set; }
}
Save method:
[Route("SaveExam")]
[HttpPost]
public IHttpActionResult SaveExam(ExamViewModel examToSave)
{
using (var db = new IntranetEntities())
{
// try to locate the desired exam to update
var selectedExam = db.exams.Where(w => w.ID == examToSave.ID).SingleOrDefault();
if (selectedExam == null)
{
return NotFound();
}
// Redacted business logic
// Map the viewmodel to the domain model
Mapper.CreateMap<ExamAnswerViewModel, exam_answer>();
Mapper.CreateMap<ExamQuestionViewModel, exam_question>().ForMember(dest => dest.exam_answer, opt => opt.MapFrom(src => src.QuestionAnswers));
Mapper.CreateMap<ExamViewModel, exam>().ForMember(dest => dest.exam_question, opt => opt.MapFrom(src => src.ExamQuestions));
var viewmodel = Mapper.Map<exam>(examToSave);
// Update exam properties
selectedExam.ExamName = viewmodel.ExamName;
selectedExam.ExamDesc = viewmodel.ExamDesc;
selectedExam.AllowBackStep = viewmodel.AllowBackStep;
selectedExam.Attempts = viewmodel.Attempts;
selectedExam.RandomizeAnswerOrder = viewmodel.RandomizeAnswerOrder;
selectedExam.exam_question = viewmodel.exam_question; // DUPLICATES PROPS
// Save
db.SaveChanges();
return Ok(examToSave);
}
}