[Column, Nullable] public DateTime? ModifiedDate { get; set; }
[Association(QueryExpressionMethod = nameof(GetfffModified), CanBeNull = true)]
public DateTime? xxxModified { get; set; }
public static Expression<Func<Offer, IDataContext, IQueryable<DateTime?>>> GetfffModified()
{
return (obj, db) => db.GetTable<OfferProducts>()
.Where(rel => rel.OfferId == obj.OfferId)
.OrderByDescending(i => i.ModifiedDate)
.Select(x => x.ModifiedDate)
.Take(1);
}
// here I am trying to set max date between ModifiedDate and xxxModified
public DateTime? Modified { get { return ModifiedDate; } set { ModifiedDate = value; } }
In above method I am trying to set the max date between ModifiedDate and xxxModified for Modified values .
Is there any possible to do like that ?
private DateTime? _FinalDate;
public DateTime? FinalDate
{
get
{
if (ModifiedDate < OfferProductsModified)
{
return OfferProductsModified;
}
else
{
return ModifiedDate;
}
}
set
{
if (ModifiedDate < OfferProductsModified)
{
_FinalDate = value;
}
else
{
//DEFAULT Value.
_FinalDate = ModifiedDate;
}
}
}
I found a way
Related
I've already made a similar application and these relationships worked - but I decided to make one change. Namely, the user can be both a dog walker and an owner. At this point the database does not want to update even though the migration is being created - (I'm using code first EF). This error pops up
Introducing FOREIGN KEY constraint 'FK_dbo.OwnerAdverts_dbo.Dogs_DogId' on table 'OwnerAdverts' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.
This is how my classes look like - there are more of them, of course, but I present those that should have relations. The Dog should have a relationship with the User, as the** User** is its Owner (there can be no dog without an owner, but an owner without a dog). Then an advertisement(OwnerAdvert) is created, where the Dog is transferred to him. In addition, there is also an announcement of the walker(WalkerAdvert) to which the User is passed. -As I wrote, a user can be both a walker and an owner.
public class User:DOGModel
{
private int userId;
private string userName;
private string userSurname;
private EnumGender userGender;
private string userEmail;
private string userPassword;
private DateTime userDateOfBirth;
private string userPhone;
[Key]
public int UserId { get => userId; set => userId = value; }
public string UserName { get => userName; set => userName = value; }
public string UserSurname { get => userSurname; set => userSurname = value; }
public EnumGender UserGender { get => userGender; set => userGender = value; }
public string UserEmail { get => userEmail; set => userEmail = value; }
public string UserPassword { get => userPassword; set => userPassword = value; }
public DateTime UserDateOfBirth { get => userDateOfBirth; set => userDateOfBirth = value; }
public string UserPhone { get => userPhone; set => userPhone = value; }
public virtual List<WalkerAdvert> advert { get; set; }
public User()
{
UserId = 0;
UserId++;
}
public User(string userName, string userSurname, EnumGender userGender, string userEmail, string userPassword, string userDateOfBirth, string userPhone):this()
{
UserName = userName;
UserSurname = userSurname;
UserGender = userGender;
UserEmail = userEmail;
UserPassword = userPassword;
DateTime.TryParseExact(userDateOfBirth, new[] { "yyyy-MM-dd", "yyyy/MM/dd", "MM/dd/yy", "dd-MMM-yy" }, null, DateTimeStyles.None, out DateTime date);
UserDateOfBirth = date;
UserPhone = userPhone;
public class Dog
{
private int dogId;
private string dogName;
private string dogDescription;
private EnumSize dogSize;
private EnumActivityDemand dogActivityDemand;
private EnumDogGender dogGender;
private string dogBreed;
private DateTime dogDateOfBirth;
[Key]
public int DogId { get => dogId; set => dogId = value; }
public string DogName { get => dogName; set => dogName = value; }
public string DogDescription { get => dogDescription; set => dogDescription = value; }
public EnumSize DogSize { get => dogSize; set => dogSize = value; }
public EnumActivityDemand DogActivityDemand { get => dogActivityDemand; set => dogActivityDemand = value; }
public EnumDogGender DogGender { get => dogGender; set => dogGender = value; }
public string DogBreed { get => dogBreed; set => dogBreed = value; }
public DateTime DogDateOfBirth { get => dogDateOfBirth; set => dogDateOfBirth = value; }
[Required]
public virtual User DogOwner { get; set; }
[Required]
public int UserId { get; set; }
public virtual List<OwnerAdvert> advert { get; set; }
public Dog()
{
DogId= 0;
DogId++;
}
public Dog(string dogName, string dogDescription, EnumSize dogSize, EnumActivityDemand dogActivityDemand, EnumDogGender dogGender, string dogBreed, string dogDateOfBirth, User dogOwner):this()
{
DogName = dogName;
DogDescription = dogDescription;
DogSize = dogSize;
DogActivityDemand = dogActivityDemand;
DogGender = dogGender;
DogBreed = dogBreed;
DateTime.TryParseExact(dogDateOfBirth, new[] { "yyyy-MM-dd", "yyyy/MM/dd", "MM/dd/yy", "dd-MMM-yy" }, null, DateTimeStyles.None, out DateTime date);
DogDateOfBirth = date;
DogOwner = dogOwner;
}
public class OwnerAdvert:DOGModel
{
private int ownerAdvertId;
private string ownerAdvertCity;
private string ownerAdvertTitle;
private string ownerAdvertDescription;
private string ownerAdvertAmount;
private DateTime ownerAdvertDateAdded;
private DateTime ownerAdvertDateOfSerice;
private int ownerAdvertWalkingTime;
[Key]
public int OwnerAdvertId { get => ownerAdvertId; set => ownerAdvertId = value; }
public string OwnerAdvertCity { get => ownerAdvertCity; set => ownerAdvertCity = value; }
public string OwnerAdvertTitle { get => ownerAdvertTitle; set => ownerAdvertTitle = value; }
public string OwnerAdvertDescription { get => ownerAdvertDescription; set => ownerAdvertDescription = value; }
public string OwnerAdvertAmount { get => ownerAdvertAmount; set => ownerAdvertAmount = value; }
public DateTime OwnerAdvertDateAdded { get => ownerAdvertDateAdded; set => ownerAdvertDateAdded = value; }
public DateTime OwnerAdvertDateOfSerice { get => ownerAdvertDateOfSerice; set => ownerAdvertDateOfSerice = value; }
public int OwnerAdvertWalkingTime { get => ownerAdvertWalkingTime; set => ownerAdvertWalkingTime = value; }
public virtual Dog Dog { get; set; }
public int DogId { get; set; }
public OwnerAdvert()
{
OwnerAdvertId = 0;
OwnerAdvertId++;
}
public OwnerAdvert( Dog ownerAdvertDog, string ownerAdvertCity, string ownerAdvertTitle, string ownerAdvertDescription, string ownerAdvertAmount, DateTime ownerAdvertDateAdded, DateTime ownerAdvertDateOfSerice, int ownerAdvertWalkingTime):this()
{
Dog = ownerAdvertDog;
OwnerAdvertCity = ownerAdvertCity;
OwnerAdvertTitle = ownerAdvertTitle;
OwnerAdvertDescription = ownerAdvertDescription;
OwnerAdvertAmount = ownerAdvertAmount;
OwnerAdvertDateAdded = ownerAdvertDateAdded;
OwnerAdvertDateOfSerice = ownerAdvertDateOfSerice;
OwnerAdvertWalkingTime = ownerAdvertWalkingTime;
}
public class WalkerAdvert
{
private int walkerAdvertId;
private string walkerAdvertTitle;
private string walkerAdvertCity;
private decimal walkerAdvertAmount;
private string walkerAdvertDescription;
private DateTime walkerAdvertDateAdded;
public string WalkerAdvertTitle { get => walkerAdvertTitle; set => walkerAdvertTitle = value; }
public string WalkerAdvertCity { get => walkerAdvertCity; set => walkerAdvertCity = value; }
public decimal WalkerAdvertAmount { get => walkerAdvertAmount; set => walkerAdvertAmount = value; }
public string WalkerAdvertDescription { get => walkerAdvertDescription; set => walkerAdvertDescription = value; }
public DateTime WalkerAdvertDateAdded { get => walkerAdvertDateAdded; set => walkerAdvertDateAdded = value; }
[Key]
public int WalkerAdvertId { get => walkerAdvertId; set => walkerAdvertId = value; }
public virtual User Walker { get; set; }
public int UserId { get; set; }
public WalkerAdvert()
{
WalkerAdvertId = 0;
WalkerAdvertId++;
}
public WalkerAdvert(string walkerAdvertTitle, string walkerAdvertCity, decimal walkerAdvertAmount, string walkerAdvertDescription, DateTime walkerAdvertDateAdded, User walkerAdvertWalker) : this()
{
WalkerAdvertTitle = walkerAdvertTitle;
WalkerAdvertCity = walkerAdvertCity;
WalkerAdvertAmount = walkerAdvertAmount;
WalkerAdvertDescription = walkerAdvertDescription;
WalkerAdvertDateAdded = walkerAdvertDateAdded;
Walker = walkerAdvertWalker;
}
Similar questions have already happened, but I can't quite reproduce them on my example, I also attach what the DOGModel looks like
public class DOGModel : DbContext
{
// Your context has been configured to use a 'DOGModel' connection string from your application's
// configuration file (App.config or Web.config). By default, this connection string targets the
// 'DOGAppModern.DOGModel' database on your LocalDb instance.
//
// If you wish to target a different database and/or database provider, modify the 'DOGModel'
// connection string in the application configuration file.
public DOGModel()
: base("name=DOGModel")
{
}
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Dog> Dogs { get; set; }
public virtual DbSet<OwnerAdvert> OwnerAdverts { get; set; }
public virtual DbSet<WalkerAdvert> WalkerAdverts { get; set; }
}
public class ExaminationModel
{
[DataMember]
public int ExaminationId { get; set; }
[DataMember]
public string ExaminationName { get; set; }
[DataMember]
public DateTime ExaminationDate { get ; set; }
}
I want to convert the every ExaminationDate to local time zone (local time zone is save in Database) how can i do that i tried some thing like
[DataMember]
public DateTime ExaminationDate
{
get { return ConvertToLocalDateTime(); }
set { }
}
public DateTime ConvertToLocalDateTime()
{
string timezone = System.Web.HttpContext.Current.Session["timezone"].ToString();
TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById(timezone);
DateTime thisDate = TimeZoneInfo.ConvertTimeFromUtc(ExaminationDate, infotime);
return thisDate;
}
I got an stcakoverflowexpection How could i do this what i am missing ? appreciate some help ?
You are getting the error because you are recursively calling ExaminationDate property. You could use a private variable instead like this below:
private DateTime examinationDate;
public DateTime ExaminationDate
{
get { return ConvertToLocalDateTime(examinationDate); }
set { examinationDate = value; }
}
public DateTime ConvertToLocalDateTime(DateTime examinationDate)
{
string timezone = TimeZone.CurrentTimeZone.StandardName;
TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById(timezone);
DateTime thisDate = TimeZoneInfo.ConvertTimeFromUtc(examinationDate, infotime);
return thisDate;
}
I have in Xamarin.Forms XAML ListView that is being sourced by a typed ItemSource:
ObservableCollection<WorkOrderActivity> serviceList =
new ObservableCollection<WorkOrderActivity>();
servicelistview.ItemsSource = serviceList;
Here is my table:
public class WorkOrderActivity
{
string id;
int workordernumber;
string activity;
string service;
string activitylocation;
float actualquantity;
string reference;
DateTime arrivaltime;
DateTime departuretime;
string remarks;
bool complete;
bool done;
string uom;
int startgauge;
int endgauge;
int contactgauge;
[JsonProperty(PropertyName = "id")]
public string Id
{
get { return id; }
set { id = value; }
}
[JsonProperty(PropertyName = "workordernumber")]
public int WorkOrderNumber
{
get { return workordernumber; }
set { workordernumber = value; }
}
[JsonProperty(PropertyName = "activity")]
public string Activity
{
get { return activity; }
set { activity = value; }
}
[JsonProperty(PropertyName = "service")]
public string Service
{
get { return service; }
set { service = value; }
}
[JsonProperty(PropertyName = "arrivaltime" )]
public DateTime ArrivalTime
{
get { return arrivaltime; }
set { arrivaltime = value; }
}
[JsonProperty(PropertyName = "departuretime")]
public DateTime DepartureTime
{
get { return departuretime; }
set { departuretime = value; }
}
[JsonProperty(PropertyName = "activitylocation")]
public string ActivityLocation
{
get { return activitylocation; }
set { activitylocation = value; }
}
[JsonProperty(PropertyName = "actualquantity")]
public float ActualQuantity
{
get { return actualquantity; }
set { actualquantity = value; }
}
[JsonProperty(PropertyName = "startgauge")]
public int StartGauge
{
get { return startgauge; }
set { startgauge = value; }
}
[JsonProperty(PropertyName = "endgauge")]
public int EndGauge
{
get { return endgauge; }
set { endgauge = value; }
}
[JsonProperty(PropertyName = "contactgauge")]
public int ContactGauge
{
get { return contactgauge; }
set { contactgauge = value; }
}
[JsonProperty(PropertyName = "reference")]
public string Reference
{
get { return reference; }
set { reference = value; }
}
[JsonProperty(PropertyName = "remarks")]
public string Remarks
{
get { return remarks; }
set { remarks = value; }
}
[JsonProperty(PropertyName = "uom")]
public string UOM
{
get { return uom; }
set { uom = value; }
}
[JsonProperty(PropertyName = "done")]
public bool Done
{
get { return done; }
set { done = value; }
}
[JsonProperty(PropertyName = "complete")]
public bool Complete
{
get { return complete; }
set { complete = value; }
}
[Version]
public string Version { get; set; }
[CreatedAt]
public DateTimeOffset CreatedAt { get; set; }
[UpdatedAt]
public DateTimeOffset UpdatedAt { get; set; }
[Deleted]
public bool Deleted { get; set; }
}
The problem occurs when I select an item from the listview, I get the correct record, but the datetime fields only have date in them, the time is zero.
private void servicelistview_ItemTapped(object sender, ItemTappedEventArgs e)
{
var p = (WorkOrderActivity)e.Item;
When I display the record in the listview it comes up with the correct time and date, it just appears that when I cast the selected item as a WorkOrderActivity.
Any ideas?
DateTime.Date returns 00:00:00; from the DateTime.Date documentation:
// Returns:
// A new object with the same date as this instance, and the time value set to 12:00:00
// midnight (00:00:00).
Example:
var dt = new DateTime(2018, 01, 14, 1, 2, 3);
Debug.WriteLine(dt.Date);
Debug.WriteLine(dt.Hour + " " + dt.Minute + " " + dt.Second);
Output:
1/14/2018 12:00:00 AM
1 2 3
I'm getting this exception:
Missing type map configuration or unsupported mapping.
Mapping types:
FollowUpActivityDTO -> Nullable1
LEST.Model.FollowUpActivityDTO -> System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Destination path:
List`1[0]
Source value:
class FollowUpActivityDTO {
Id: 75e83860-65e4-11e5-9a90-382c4ab9e433
Timestamp: 28/09/2015 11:28:55
StartTimestamp: 28/09/2015 15:26:00
DueTimestamp: 28/09/2015 15:26:00
ClosingTimestamp: 28/09/2015 15:26:00
Matter: elasticsearch - copia - copia.jar
Comment: Archive
Status: open
BacklogStatus: unknown
}
{Trying to map FollowUpActivityDTO to Nullable`1.}
It raises when I perform:
return AutoMapper.Mapper.Map<List<LEST.Model.FollowUpActivityDTO>, List<Domain.FollowUpActivity>>(dtos);
This is destination class:
public class FollowUpActivity
{
private String id; // Generated on server
private DateTime? creationDate;
private DateTime? startDate;
private DateTime? dueDate;
private DateTime? closingDate;
private String matter;
private String comment;
private FollowUpActivityStatus status;
private FollowUpActivityBacklogStatus backlogStatus;
private List<MetaInfoValue> metainfos;
private List<MetaResource> resources;
And this is the source class:
public class FollowUpActivityDTO
{
public FollowUpActivityDTO();
public string BacklogStatus { get; set; }
public string ChannelId { get; set; }
public DateTime? ClosingTimestamp { get; set; }
public string Comment { get; set; }
public DateTime? DueTimestamp { get; set; }
public string Id { get; set; }
public string Matter { get; set; }
public DateTime? StartTimestamp { get; set; }
public string Status { get; set; }
public DateTime? Timestamp { get; set; }
And mapping profile:
AutoMapper.Mapper.CreateMap<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>()
.ForMember(dst => dst.CreationDate, opts => opts.Ignore()) //opts.MapFrom(s => s.Timestamp.HasValue ? s.Timestamp.Value : DateTime.MinValue))
.ForMember(dst => dst.StartDate, opts => opts.Ignore()) //opts.MapFrom(s => s.StartTimestamp.HasValue ? s.StartTimestamp.Value : DateTime.MinValue))
.ForMember(dst => dst.DueDate, opts => opts.Ignore()) //opts.MapFrom(s => s.DueTimestamp.HasValue ? s.DueTimestamp.Value : DateTime.MinValue))
.ForMember(dst => dst.ClosingDate, opts => opts.Ignore()) //opts.MapFrom(s => s.ClosingTimestamp.HasValue ? s.ClosingTimestamp.Value : DateTime.MinValue));
.ForMember(dst => dst.Status, opts => opts.UseValue<Domain.FollowUpActivityStatus>(Domain.FollowUpActivityStatus.Open))
.ForMember(dst => dst.BacklogStatus, opts => opts.UseValue<Domain.FollowUpActivityBacklogStatus>(Domain.FollowUpActivityBacklogStatus.Work));
I've created a Test for testing this mapping:
namespace Tests
{
[TestFixture]
public class Mapping
{
[SetUp]
public void initialize()
{
Core.Mappings.AutoMapperConfiguration.Configure();
}
[Test]
public void configutation()
{
AutoMapper.Mapper.AssertConfigurationIsValid<Core.Mappings.Profiles.FollowUpActivityProfile>();
}
[Test]
public void followUpActivityDTOToDOMAIN()
{
LEST.Model.FollowUpActivityDTO dto = new LEST.Model.FollowUpActivityDTO()
{
Id = new Guid().ToString(),
Timestamp = DateTime.UtcNow,
StartTimestamp = DateTime.UtcNow,
DueTimestamp = DateTime.UtcNow,
ClosingTimestamp = DateTime.UtcNow,
Matter = "Matter",
Comment = "Comment",
Status = "open",
BacklogStatus = "work",
Metainfos = new System.Collections.Generic.List<LEST.Model.MetaInfoValueDTO>()
};
Domain.FollowUpActivity domain = new Domain.FollowUpActivity();
AutoMapper.Mapper.Map<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>(dto, domain);
domain.Should().NotBeNull();
domain.Id.Should().Be(dto.Id);
}
}
}
Take a look on:
Domain.FollowUpActivity domain = new Domain.FollowUpActivity();
AutoMapper.Mapper.Map<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>(dto, domain);
Now I works. However, if I code as follow, it crashes:
Domain.FollowUpActivity domain = AutoMapper.Mapper.Map<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>(dto);
I'm figuring out the problem is over the constructors of my Destination class then:
namespace Domain
{
public enum FollowUpActivityStatus
{
Open,
Closed,
Delegated
}
public enum FollowUpActivityBacklogStatus
{
Work,
Sprint
}
public class FollowUpActivity
{
private String id; // Generated on server
private DateTime? creationDate;
private DateTime? startDate;
private DateTime? dueDate;
private DateTime? closingDate;
private String matter;
private String comment;
private FollowUpActivityStatus status;
private FollowUpActivityBacklogStatus backlogStatus;
private List<MetaInfoValue> metainfos;
private List<MetaResource> resources;
#region Properties
public String Id
{
get { return id; }
set { id = value; }
}
public DateTime? CreationDate
{
get { return creationDate; }
set { creationDate = value; }
}
public DateTime? StartDate
{
get { return startDate; }
set { startDate = value; }
}
public DateTime? DueDate
{
get { return dueDate; }
set { dueDate = value; }
}
public DateTime? ClosingDate
{
get { return closingDate; }
set { closingDate = value; }
}
public FollowUpActivityStatus Status
{
get { return status; }
set { status = value; }
}
public FollowUpActivityBacklogStatus BacklogStatus
{
get { return backlogStatus; }
set { backlogStatus = value; }
}
public String Matter
{
get { return matter; }
set { matter = value; }
}
public String Comment
{
get { return comment; }
set { comment = value; }
}
public List<MetaInfoValue> Metainfos
{
get { return metainfos; }
set { metainfos = value; }
}
public List<MetaResource> Resources
{
get { return resources; }
set { resources = value; }
}
#endregion
#region Constructors
public FollowUpActivity()
: this(null, null)
{
}
public FollowUpActivity(String matter, String comment = null, params Domain.MetaInfoValue[] metainfos)
: this(DateTime.Now, matter, comment, metainfos)
{
}
public FollowUpActivity(DateTime creationDate, String matter, String comment = null, params Domain.MetaInfoValue[] metainfos)
: this(creationDate, matter, new List<MetaResource>(), comment, metainfos)
{
}
public FollowUpActivity(DateTime creationDate, String matter, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
: this(creationDate, matter, FollowUpActivityBacklogStatus.Work, new List<MetaResource>(), comment, metainfos)
{
}
public FollowUpActivity(DateTime creationDate, String matter, FollowUpActivityBacklogStatus backlogStatus, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
: this(creationDate, null, null, null, matter, FollowUpActivityStatus.Open, backlogStatus, new List<MetaResource>(), comment, metainfos)
{
}
public FollowUpActivity(DateTime? creationDate, DateTime? startDate, DateTime? dueDate, DateTime? closingDate, String matter, FollowUpActivityStatus status, FollowUpActivityBacklogStatus backlogStatus, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
{
this.id = String.Empty;
this.creationDate = creationDate;
this.startDate = startDate;
this.dueDate = dueDate;
this.closingDate = closingDate;
this.matter = matter;
this.comment = comment;
this.status = status;
this.backlogStatus = backlogStatus;
this.metainfos = new List<MetaInfoValue>(metainfos);
this.resources = resources;
}
#endregion
}
}
However, I don't know why does it crashes using the second approach... Someone knows why?
Thanks for all.
I am setting up default value for CreatedDate in my entity class as shown below. But during implementation I get the value 01/01/0001
private DateTime _createdDate = DateTime.Now;
private DateTime _modifiedDate = DateTime.Now;
public DateTime CreatedDate
{
get { return _createdDate; }
set { _createdDate = value; }
}
public string LastModifiedBy { get; set; }
public DateTime LastModifiedDate
{
get { return _modifiedDate; }
set { _modifiedDate = value; }
}
here is my implementation
public bool AddJewelryItem(List<Entities.Jewelry> jewelryList)
{
bool isSuccess = false;
try
{
using (var db = new Context())
{
foreach (Entities.Jewelry jewelry in jewelryList)
{
db.JewelryItemsList.Add(jewelry.jewelryItems);
db.SaveChanges();
jewelry.jewelryAddnlDetails.JewelryID = jewelry.jewelryItems.JewelryID;
if (jewelry.jewelryAddnlDetails.JewelryID != 0)
{
db.JewelryAddnlDetailsList.Add(jewelry.jewelryAddnlDetails);
db.SaveChanges();
}
}
isSuccess = true;
}
}