How can I track created and modified date automatically in C# with MongoDB.
I have database:
public class BaseEntity
{
public DateTime? DateCreated { get; set; }
public string UserCreated { get; set; }
public DateTime? DateModified { get; set; }
public string UserModified { get; set; }
}
public class Income : BaseEntity
{
public Guid Id { get; set; }
[DisplayName("Income")]
public string Income_ { get; set; }
public string Date { get; set; }
public String Description { get; set; }
}
I want to track DateCreated and DateModified automatically.
Related
I have two tables which have one-to-many relationship between them.
public class Policy : BaseEntityAudit
{
public override string Kod { get; set; }
public PolicyType PolicyType { get; set; } = PolicyType.Policy;
public long AgentId { get; set; }
public long InsuranceTypeId { get; set; }
public string PolicyNu { get; set; }
public long OwnerId { get; set; }
public string PlateNumber { get; set; }
public string Explanation { get; set; }
public Agent Agent { get; set; }
public InsuranceType InsuranceType { get; set; }
public Owner Owner { get; set; }
public virtual ICollection<SubPolicy> SubPolicies { get; set; }
}
public class SubPolicy : BaseEntityAudit
{
public override string Kod { get; set; }
public long PolicyId { get; set; }
public DateTime IssueDate { get; set; } = DateTime.Now.Date;
public DateTime StartDate { get; set; } = DateTime.Now.Date;
public DateTime EndDate { get; set; } = DateTime.Now.AddYears(1);
public decimal Premium { get; set; }
public long InsurerId { get; set; }
[StringLength(50)]
public Policy Policy { get; set; }
public Insurer Insurer { get; set; }
}
How can I insert related records of these tables to database under one side of one-to-many relationship, so under Policy entity?
Attention:I'm using EF Code-First model, not Db-First;
In my previous question, I've mentioned that I'm developing a Point of Sale application. The platforms I use are WPF, MVVM and Entity Framework (code-first). Since Entity Framework code-first has its own logic to map entities and relations, I'm confused about a case.
I have an EntityBase class:
public class EntityBase
{
public DateTime? CreatedAt { get; set; }
public User CreatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
public User UpdatedBy { get; set; }
}
public class User : EntityBase
{
public int UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
Since User class has EntityBase class members, will it arise problems at database side?
Or should I keep the data like this:
public class EntityBase
{
public DateTime? CreatedAt { get; set; }
public int CreatedByUserId { get; set; }
public DateTime? UpdatedAt { get; set; }
public int UpdatedByUserId { get; set; }
}
Create your models in this way:
public class EntityBase
{
public DateTime? CreatedAt { get; set; }
public int CreatedById { get; set; }
[ForeignKey("CreatedById")]
public User CreatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
public int UpdatedById { get; set; }
[ForeignKey("UpdatedById")]
public User UpdatedBy { get; set; }
}
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
I have that model class:
public class CV
{
public PrivateInformation privateInformation { get; set; }
public List<Education> education { get; set; }
public List<WorkingExperience> workingExperience { get; set; }
}
public class PrivateInformation
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
}
public class Education
{
public string UniversityName { get; set; }
public string Faculty { get; set; }
public string Specialization { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
}
public class WorkingExperience
{
public string CompanyName { get; set; }
public string Position { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
}
and there are another classes too. I want to make that user enter whole information from one page. (university can be more than 1 because I have list. working experience too).
But when I'm making CV as model class in view I can't access to Education.Name
How can I do that? Or is there any other way?
The common pattern for doing that is creating a ViewModel class
Basically, create a class with all the properties you need for your view, and use it.
https://msdn.microsoft.com/en-us/library/ff798384.aspx
Just create a view Model of entire model and initilize properties of other classes in it if you want to use other propeerties also like,
Public class PrivateInformationViewModel
{
ublic string UniversityName { get; set; }
public string Faculty { get; set; }
public string Specialization { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
public Education EModel {get; set;}
}
Now through this view model you can access education properties also.
I am trying to force entity framework 5.0 to create a constructor on each of my generated poco classes. This constructor should instantiate any foreign key navigation properties I have.
e.g.
public partial class Event
{
public System.Guid EventId { get; set; }
public System.DateTime CreatedDate { get; set; }
public string CreatedUser { get; set; }
public int CreatedUserId { get; set; }
public string Title { get; set; }
public string EventDesc { get; set; }
public System.DateTime Start { get; set; }
public System.DateTime End { get; set; }
public string Source { get; set; }
public bool Editable { get; set; }
public string ClassName { get; set; }
public string Url { get; set; }
public bool IsDeleted { get; set; }
public bool IsObsolete { get; set; }
public bool AllDay { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedUser { get; set; }
public int RowVer { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
should become:
public partial class Event
{
public Event()
{
this.UserProfile = new UserProfile();
}
public System.Guid EventId { get; set; }
public System.DateTime CreatedDate { get; set; }
public string CreatedUser { get; set; }
public int CreatedUserId { get; set; }
public string Title { get; set; }
public string EventDesc { get; set; }
public System.DateTime Start { get; set; }
public System.DateTime End { get; set; }
public string Source { get; set; }
public bool Editable { get; set; }
public string ClassName { get; set; }
public string Url { get; set; }
public bool IsDeleted { get; set; }
public bool IsObsolete { get; set; }
public bool AllDay { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedUser { get; set; }
public int RowVer { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
I know it is possible, but not sure how. Any help would be most appreciated.
Thanks
When I retrieve from db in my repository (see below) I create a list of events, when I pass this list of events back via json I get a parse error due to event.UserProfile being null.
I could set it in code for each event, but that wouldn't be smart.
I need a link or an example if possible to help achieve what I need.
public List<Event> GetEvents(int userId, DateTime start, DateTime end)
{
List<Event> domainList = new List<Event>();
using (BookingModels dbEntities = new BookingModels())
{
var eventQuery = from dboEvents in dbEntities.Events
where dboEvents.Start >= start
&& dboEvents.End <= end
select dboEvents;
domainList = eventQuery.ToList<Event>();
}
return domainList;
}
I am having trouble with SQL Server creating Foreign Key constraints when using Entity Framework Code First.
This is my scenario. I am building an application which allows us to log tickets against any of our systems and automatically assign the ticket to the relevant person.
We have Services, which can have many categories. The categories can have many subcategories. A help desk person can be assigned to Service, and Category or Subcategory.
Here are my classes:
Service.cs
public class Service
{
[Key]
public int ServiceID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Subcategory> Subcategories { get; set; }
public virtual ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
Category.cs
public class Category
{
[Key]
public int CategoryID { get; set; }
[ForeignKey("Service")]
public int ServiceID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public virtual Service Service { get; set; }
public virtual ICollection<Subcategory> Subcategories { get; set; }
public virtual ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
Subcategory.cs
public class Subcategory
{
[Key]
public int SubcategoryID { get; set; }
[ForeignKey("Service")]
public int ServiceID { get; set; }
[ForeignKey("Category")]
public int CategoryID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public virtual Service Service { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
and finally HelpDeskMember.cs
public class HelpDeskMember
{
public int HelpDeskMemberID { get; set; }
public string Name { get; set; }
public bool Admin { get; set; }
public bool Available { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? LastLogin { get; set; }
public DateTime? DeletedDate { get; set; }
public DateTime? DeletedBy { get; set; }
public virtual ICollection<Service> Services { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Subcategory> Subcategories { get; set; }
}
When the Database is being initialised, I am getting the following error message:
Introducing FOREIGN KEY constraint 'Subcategory_Service' on table 'Subcategories' 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. See previous errors.
I'm guessing the problem is with how I have defined the Key's and ForeignKey's. Any help appreciated. Thanks.
Depending on your needs. You can in configuration for your entity use WillCascadeOnDelete(false)1 or globally removing OneToManyCascadeDeleteConvention2.
Both can be set in OnModelCreating using ModelBuilder input parameter.