Getting the most recent linked entity in a different entity - c#

Sorry for the confusing title, if anyone knows a better summary please edit.
I have a Premise and a Person. A person can own a premise, which is then called a Property.
This is what my entities look like:
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public virtual List<Property> Properties { get; set; }
}
public class Premise
{
public int PremiseId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
public class Property
{
public virtual Premise Premise { get; set; }
public virtual Person Owner { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
}
So now I can easily get the properties a person has / had.
But can I get the CurrentOwner from a Premise as well?
The current owner would be the Person of the most recent Property.
I'd think it'd go something like this:
public class Premise
{
public int PremiseId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public virtual Person CurrentOwner
{
get
{
return Properties.Where(p => p.FromDate => today && p.ToDate <= today).FirstOrDefault();
}
}
}
But that won't work because I don't have a Properties list near.

Related

nPoco - Get single object with nested data

I have two Dto's:
[TableName("Address")]
public class AddressDto
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string Building { get; set; }
public string Appartment { get; set; }
public string ZipCode { get; set; }
public string Floor { get; set; }
public DateTime CreatedDate { get; set; }
}
[TableName("DistributionPoint")]
public class DistributionPointDto
{
[Column("Id")]
public int Id { get; set; }
public string Name { get; set; }
[Reference(ReferenceType.Foreign, ColumnName = "AddressId", ReferenceMemberName = "Id")]
public AddressDto Address { get; set; }
}
How can I get DistributionPointDto with nested AddressDto using nPoco?
I have a generic repository for CRUD with method:
public T FindById<T>(int id)
{
return _db.SingleById<T>(id);
}
But, when I'm trying to get DistributionPointDto, AddressDto is null
Curious if you got your code to work, but I have found that you need to use Query instead of SingleById to include referenced properties, and then you can use an Include statement/clause, however I don't think generics work with this, you have to explicitly mention the referenced property name, which is AddressDTO for you.
_db.Query<DistributionPointDTO>()
.Include(x => x.AddressDTO)
.Where(x => x.Id == id).First();

How to fetch an attributes from my class with EF

I have 3 class, 2 are simple class and 1 combines the two of them.
public class Person:Base
{
public DateTime DOB { get; set; }
public string first { get; set; }
public string last { get; set; }
}
public class Roles:Base
{
public string Description { get; set; }
}
public class RolePerson:Base
{
public int pID { get; set; }
public virtual Person PersonID { get; set; }
public int rID { get; set; }
public virtual Roles RoleID { get; set; }
public int order { get; set; }
}
Now I have method that would select RolePerson witch would grab all member of pID with the id that i provide in the argument, and return the RoleID field.
this is how my method look like
public IEnumerable<Roles> PersonRoles(int personID)
{
return db.RolePerson.Where<RolePerson>((p) => p.pID == personID);
}
How can i say to return the role and not the roleperson.
Update:
My base class is as follow
public abstract class Base
{
public int ID { get; set; }
public Boolean isValid { get; set; }
public DateTime createdOn { get; set; }
public int createdID { get; set; }
public Person createdPerson { get; set; }
public DateTime updatedOn { get; set; }
public int updatedID { get; set; }
public Person updatedPerson { get; set; }
}
You can run a select statement in the end.
return d.RolePerson.Where(p => p.PId == personID).Select(r=> r.RoleID);

use many model class in view

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.

Code-First SQL Server ASP.NET MVC6

I am a VB.NET programmer, but I am trying to learn C# and MVC in my spare time. I am using ASP.NET MVC 5.1.0.0 and I am trying to do code-First database creation in a local instance of SQL Server.
I was able to get the first database table to update in the database when I ran Update-Database from within the IDE, but when I added a second table that has a PK/FK relationship with the first, I am getting a red line under [ForeignKey] which reads
Does not contain a constructor that takes 1 arguments
I have been searching all over and not getting anywhere. Any suggestions or help would be appreciated. By the way, the first table is a PK/FK relationship to the AspNetUsers table.
public class BuildDatabase : IdentityUser
{
public virtual Companies Companies { get; set; }
public virtual NotaryProfile NotaryProfile { get; set; }
}
public class Companies
{
[Key]
[Column("CompanyID")] // Did this as the database will reflect TableName_ColumnName instead.
public int CompanyID { get; set; }
public string CompanyName { get; set; }
public bool IsActive { get; set; }
public bool IsNotary { get; set; }
public virtual ICollection<NotaryProfile> NotaryProfile { get; set; }
}
public class NotaryProfile
{
[Key]
public int NotaryID { get; set; }
public string NamePrefix { get; set; }
public string FirstName { get; set; }
public string MiddleInitial { get; set; }
public string LastName { get; set; }
public string NameSuffix { get; set; }
public bool IsActive { get; set; }
public int DefaultState { get; set; }
public int DefaultCounty { get; set; }
public bool IsSigningAgent { get; set; }
public bool HasABond { get; set; }
public decimal BondAmount { get; set; }
public bool HasEandO { get; set; }
public decimal EandOAmount { get; set; }
public bool ElectronicNotarizationsAllowed { get; set; }
public string ElectronicTechnologyUsed { get; set; }
public string ComissionNumber { get; set; }
public DateTime CommissionIssued { get; set; }
public DateTime CommssionOriginal { get; set; }
public DateTime CommissionExpires { get; set; }
public DateTime CommissionFiledOn { get; set; }
public string SOSAuditNumber { get; set; }
public string CommissionDesc { get; set; }
[Foreignkey("CompanyID")] // Companies.CompanyID = PK
public int CompanyID { get; set; } // PK/FK relationship.
public Companies Companies { get; set; } // Reference to Companies table above.
}
public class SchemaDBContext : IdentityDbContext<BuildDatabase>
{
public SchemaDBContext()
: base("DefaultConnection"){}
public DbSet<Companies> Companies { get; set; }
public DbSet<NotaryProfile> NotaryProfile { get; set; }
}
One of your classes (probably NotaryProfile) needs to reference another object (the foreign key relationship) but there is no constructor in that class that accepts an argument to establish that relationship, e.g.:
public NotaryProfile(int companyId) {
this.companyId = companyId;
}
BTW, a better way to establish that relationship is to use the actual class type rather than the ID, as in:
public class NotaryProfile {
...
public Company Company { get; set; }
// Instead of this:
// public int CompanyID { get; set; } // PK/FK relationship.
...
}
See also:
C# “does not contain a constructor that takes '1' arguments”
Does not contain a constructor that takes 2 arguments

Asp.Net MVC Reaching The Property Using Many To Many Relation With Mapping Table

You can see my previous question which related with many to many relation but with auto generated mapping table.
I have 2 model, HrTraining and HrPerson. Any people can be assigned to one or more Trainings. You can see my model as below
public class HrTraining
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}
public class HrMapTrainingPerson
{
public int Id { get; set; }
public string Status { get; set; }
public int HrTrainingId { get; set; }
public int HrPersonId { get; set; }
public virtual HrTraining HrTraining { get; set; }
public virtual HrPerson HrPerson { get; set; }
}
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}
How can i take all training objects which assingned to a person with efficient way.
So you want to find a person, and get all the trainings assigned to it? There are lot of ways.. but using your models, this could be something like this
var trPersons = dbContext.HrPerson.Find(idPerson).HrMapTrainingPerson.ToList();
foreach(var trPerson in trPersons) {
var training = trPerson.HrTraining;
//do what you want, here you can get trPerson.HrTraining.Name for instance
}

Categories