Automapper with Entity Framework, Initialization Error - entity-framework

I have an EF6 MVC app using Code First to generate the models. I am trying to create a mapping using AutoMapper between the model and a view model representation of that model. However, when I do the mapping I receive the error:
Mapper not initialized. Call Initialize with appropriate
configuration. If you are trying to use mapper instances through a
container or otherwise, make sure you do not have any calls to the
static Mapper.Map methods, and if you're using ProjectTo or
UseAsDataSource extension methods, make sure you pass in the
appropriate IConfigurationProvider instance.
I am definitely initializing the Mapper, but I am not able to figure out why the configuration is failing. Could anyone provide some assistance on what I can do to successfully use AutoMapper in this situation?
Here is the code for how I am configuring and initializing the Mapper:
var pTConnections = _db.PTConnections.Include(p => p.PTConnectionClass).ToList();
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PTConnection, PTConnectionViewModel>()
.AfterMap((s, d) => { foreach (var i in d.PTCredentialAssignments) i.PTConnection = d; });
cfg.CreateMap<PTCredentialAssignment, PTCredentialAssignmentViewModel>()
.ForMember(m => m.PTCredential, opt => opt.Ignore())
.ForMember(m => m.PTConnection, opt => opt.Ignore());
cfg.CreateMap<PTVendor, PTVendorViewModel>()
.ForMember(m => m.PTCredentials, opt => opt.Ignore())
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTCredential, PTCredentialViewModel>();
cfg.CreateMap<PTConnectionClass, PTConnectionClassViewModel>()
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTConnectionContactAssignment, PTConnectionContactAssignmentViewModel>()
.ForMember(m => m.PTConnection, opt => opt.Ignore());
});
var dest = Mapper.Map<List<PTConnection>, List<PTConnectionViewModel>>(pTConnections);
Here is my Code First Model:
public partial class PTConnection
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTConnection()
{
PTConnectionAssignments = new HashSet<PTConnectionAssignment>();
PTCredentialAssignments = new HashSet<PTCredentialAssignment>();
PTConnectionContactAssignments = new HashSet<PTConnectionContactAssignment>();
}
[Key]
public int PTCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(255)]
public string ConnectionName { get; set; }
[Required]
[StringLength(100)]
public string Host { get; set; }
public int Port { get; set; }
public string Comment { get; set; }
public int PTCCID { get; set; }
public bool? IsDisabled { get; set; }
public int PTVID { get; set; }
public virtual PTConnectionClass PTConnectionClass { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnectionAssignment> PTConnectionAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialAssignment> PTCredentialAssignments { get; set; }
public virtual PTVendor PTVendor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnectionContactAssignment> PTConnectionContactAssignments { get; set; }
}
public partial class PTCredential
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTCredential()
{
PTCredentialAssignments = new HashSet<PTCredentialAssignment>();
PTCredentialContactAssignments = new HashSet<PTCredentialContactAssignment>();
}
[Key]
public int PTCRID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(100)]
public string CredUsername { get; set; }
[Required]
[StringLength(500)]
public string CredPassword { get; set; }
public string Directory { get; set; }
public bool? IsDisabled { get; set; }
public string Comments { get; set; }
public int? PTVID { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialAssignment> PTCredentialAssignments { get; set; }
public virtual PTVendor PTVendor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialContactAssignment> PTCredentialContactAssignments { get; set; }
}
public partial class PTVendor
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTVendor()
{
PTConnections = new HashSet<PTConnection>();
PTCredentials = new HashSet<PTCredential>();
PTIneligableCredentialVendorAssignments = new HashSet<PTIneligableCredentialVendorAssignment>();
PTVendorAssignments = new HashSet<PTVendorAssignment>();
PTVendorContactAssignments = new HashSet<PTVendorContactAssignment>();
}
[Key]
public int PTVID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50), Display(Name="Vendor Name")]
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnection> PTConnections { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredential> PTCredentials { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTIneligableCredentialVendorAssignment> PTIneligableCredentialVendorAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTVendorAssignment> PTVendorAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTVendorContactAssignment> PTVendorContactAssignments { get; set; }
}
[Table("PTCredentialAssignment")]
public partial class PTCredentialAssignment
{
[Key]
public int PTCRAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
public int PTCRID { get; set; }
public virtual PTConnection PTConnection { get; set; }
public virtual PTCredential PTCredential { get; set; }
}
[Table("PTConnectionClass")]
public partial class PTConnectionClass
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTConnectionClass()
{
PTConnections = new HashSet<PTConnection>();
}
[Key]
public int PTCCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50)]
public string Class { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnection> PTConnections { get; set; }
}
[Table("PTConnectionContactAssignment")]
public partial class PTConnectionContactAssignment
{
[Key]
public int PTCCAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
[Required]
[StringLength(60)]
public string FirstName { get; set; }
[Required]
[StringLength(60)]
public string LastName { get; set; }
[Required]
[StringLength(10)]
public string PhoneNumber { get; set; }
[StringLength(20)]
public string Extension { get; set; }
[StringLength(100)]
public string Email { get; set; }
[StringLength(60)]
public string Title { get; set; }
public string Comment { get; set; }
public virtual PTConnection PTConnection { get; set; }
}
Here is my ViewModel:
public partial class PTConnectionViewModel
{
[Key]
public int PTCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(255)]
public string ConnectionName { get; set; }
[Required]
[StringLength(100)]
public string Host { get; set; }
public int Port { get; set; }
public string Comment { get; set; }
public int PTCCID { get; set; }
public bool? IsDisabled { get; set; }
public int PTVID { get; set; }
public virtual PTConnectionClassViewModel PTConnectionClass { get; set; }
public virtual ICollection<PTConnectionAssignmentViewModel> PTConnectionAssignments { get; set; }
public virtual ICollection<PTCredentialAssignmentViewModel> PTCredentialAssignments { get; set; }
public virtual PTVendorViewModel PTVendor { get; set; }
public virtual ICollection<PTConnectionContactAssignmentViewModel> PTConnectionContactAssignments { get; set; }
}
public partial class PTVendorViewModel
{
[Key]
public int PTVID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50), Display(Name="Vendor Name")]
public string Name { get; set; }
public virtual ICollection<PTConnectionViewModel> PTConnections { get; set; }
public virtual ICollection<PTCredentialViewModel> PTCredentials { get; set; }
}
public partial class PTCredentialViewModel
{
[Key]
public int PTCRID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(100)]
public string CredUsername { get; set; }
[Required]
[StringLength(500)]
public string CredPassword { get; set; }
public string Directory { get; set; }
public bool? IsDisabled { get; set; }
public string Comments { get; set; }
public int? PTVID { get; set; }
public virtual ICollection<PTCredentialAssignmentViewModel> PTCredentialAssignments { get; set; }
public virtual PTVendorViewModel PTVendor { get; set; }
public virtual ICollection<PTCredentialContactAssignmentViewModel> PTCredentialContactAssignments { get; set; }
}
public partial class PTCredentialAssignmentViewModel
{
[Key]
public int PTCRAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
public int PTCRID { get; set; }
public virtual PTConnectionViewModel PTConnection { get; set; }
public virtual PTCredentialViewModel PTCredential { get; set; }
}
public partial class PTConnectionClassViewModel
{
[Key]
public int PTCCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50)]
public string Class { get; set; }
public virtual ICollection<PTConnectionViewModel> PTConnections { get; set; }
}
public partial class PTConnectionContactAssignmentViewModel
{
[Key]
public int PTCCAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
[Required]
[StringLength(60)]
public string FirstName { get; set; }
[Required]
[StringLength(60)]
public string LastName { get; set; }
[Required]
[StringLength(10)]
public string PhoneNumber { get; set; }
[StringLength(20)]
public string Extension { get; set; }
[StringLength(100)]
public string Email { get; set; }
[StringLength(60)]
public string Title { get; set; }
public string Comment { get; set; }
public virtual PTConnectionViewModel PTConnection { get; set; }
}

Just create the map from one type to another. Don't create the map from a List of one type to a list of another type. Automapper can automatically handle collections.
In effect, you're telling Automapper how to map a List<A> to a List<B> without telling Automapper how to actually map A to B. Change your registration to the following:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PTConnection, PTConnectionViewModel>();
});
You can still map a List<PTConnection> to a List<PTConnectionViewModel> just fine, as well as most other collection types.

Related

Code first one-to-many relationship with restraints

I have the following one-to-many database structure generated by code first. Many Buttons can be added with a relation back to a single GSMUnit. BtnNo should be unique (for every GSMUnitId), and shouldn't be duplicated. Currently it is possible to duplicate. how can i prevent this?
public class GSMUnit
{
public int Id { get; set; }
[MaxLength(20)]
public string Model { get; set; }
[MaxLength(40)]
public string GsmName { get; set; }
[MaxLength(40)]
public string TelephoneNum { get; set; }
public int GSMSiteId { get; set; }
public virtual GSMSite GSMSite { get; set; }
public virtual GSMSetting GSMSettings { get; set; }
public virtual ICollection<Button> Buttons { get; set; }
}
public class Button
{
public int Id { get; set; }
[MaxLength(30)]
public string Primary { get; set; }
[MaxLength(30)]
public string Divert1 { get; set; }
[MaxLength(30)]
public string Divert2 { get; set; }
[MaxLength(30)]
public string Divert3 { get; set; }
[MaxLength(20)]
public string AptNo { get; set; }
[MaxLength(10)]
public string Code { get; set; }
[MaxLength(4)]
public string DTO { get; set; }
[MaxLength(4)]
public string Timeband { get; set; }
[MaxLength(15)]
public string BtnNo { get; set; }
[MaxLength(20)]
public string AptName { get; set; }
public int GSMUnitId { get; set; }
public virtual GSMUnit GSMUnit { get; set; }
}
You can use the Index attribute for that:
public class Button
{
public int Id { get; set; }
[MaxLength(30)]
public string Primary { get; set; }
[MaxLength(30)]
public string Divert1 { get; set; }
[MaxLength(30)]
public string Divert2 { get; set; }
[MaxLength(30)]
public string Divert3 { get; set; }
[MaxLength(20)]
public string AptNo { get; set; }
[MaxLength(10)]
public string Code { get; set; }
[MaxLength(4)]
public string DTO { get; set; }
[MaxLength(4)]
public string Timeband { get; set; }
[MaxLength(15)]
public string BtnNo { get; set; }
[MaxLength(20)]
public string AptName { get; set; }
[Index(IsUnique = true)]
public int GSMUnitId { get; set; }
public virtual GSMUnit GSMUnit { get; set; }
}
But I recommend you to take a look on Fluent API. It's easier to configure in my opinion.

Error in creating a controller file

I am using Entity Framework. I have tried everything, searching and adding keys but Ienter image description here cannot understand what the problem is and how to resolve it.
public class Reservation
{
[Key]
public int BookingID { get; set; }
public int CustomerID { get; set; }
public int RoomID { get; set; }
public string BookingDate { get; set; }
public int Check_In { get; set; }
public int Check_Out { get; set; }
public int Adults { get; set; }
public int Children { get; set; }
public int NoOfNights { get; set; }
[ForeignKey("RoomID")]
public virtual Room Rooms { get; set; }
[ForeignKey("CustomerID")]
public virtual CustomerDetails CustomerDetail { get; set; }
public virtual ICollection<Payment> Payment { get; set; }
}
public class CustomerDetails
{
[Key]
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int PostCode { get; set; }
public string State { get; set; }
public int PhoneNumber { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual ICollection<Reservation> Reservations { get; set; }
}
enter image description here
All tables need a primary key or you can't use Entity Framework.

AutoMapper with EF6 not populating one of the mapped objects

I have an EF6 MVC app using Code First to generate the models. I am trying to create a mapping using AutoMapper between the model and a viewmodel representation of that model.
I don't have errors, but the resulting object is missing details for the PTCredential object. Specifically I am missing the PTCredential object that is nested within a PTCredential.
I don't believe the Ignores are causing this, but I can't pin it down. I have tried some suggestions from other posts on AutoMapper, but nothing has proven useful.
Could anyone provide some assistance on what I can do to successfully use AutoMapper in this situation?
Here is the code for how I am configuring and initializing the Mapper:
var pTConnections = _db.PTConnections.Include(p => p.PTConnectionClass).ToList();
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PTConnection, PTConnectionViewModel>()
.AfterMap((s, d) => { foreach (var i in d.PTCredentialAssignments) i.PTConnection = d; });
cfg.CreateMap<PTCredentialAssignment, PTCredentialAssignmentViewModel>()
.ForMember(m => m.PTCredential, opt => opt.Ignore())
.ForMember(m => m.PTConnection, opt => opt.Ignore());
cfg.CreateMap<PTVendor, PTVendorViewModel>()
.ForMember(m => m.PTCredentials, opt => opt.Ignore())
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTCredential, PTCredentialViewModel>();
cfg.CreateMap<PTConnectionClass, PTConnectionClassViewModel>()
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTConnectionContactAssignment, PTConnectionContactAssignmentViewModel>()
.ForMember(m => m.PTConnection, opt => opt.Ignore());
});
var dest = Mapper.Map<List<PTConnection>, List<PTConnectionViewModel>>(pTConnections);
Here is my Code First Model:
public partial class PTConnection
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTConnection()
{
PTConnectionAssignments = new HashSet<PTConnectionAssignment>();
PTCredentialAssignments = new HashSet<PTCredentialAssignment>();
PTConnectionContactAssignments = new HashSet<PTConnectionContactAssignment>();
}
[Key]
public int PTCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(255)]
public string ConnectionName { get; set; }
[Required]
[StringLength(100)]
public string Host { get; set; }
public int Port { get; set; }
public string Comment { get; set; }
public int PTCCID { get; set; }
public bool? IsDisabled { get; set; }
public int PTVID { get; set; }
public virtual PTConnectionClass PTConnectionClass { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnectionAssignment> PTConnectionAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialAssignment> PTCredentialAssignments { get; set; }
public virtual PTVendor PTVendor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnectionContactAssignment> PTConnectionContactAssignments { get; set; }
}
public partial class PTCredential
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTCredential()
{
PTCredentialAssignments = new HashSet<PTCredentialAssignment>();
PTCredentialContactAssignments = new HashSet<PTCredentialContactAssignment>();
}
[Key]
public int PTCRID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(100)]
public string CredUsername { get; set; }
[Required]
[StringLength(500)]
public string CredPassword { get; set; }
public string Directory { get; set; }
public bool? IsDisabled { get; set; }
public string Comments { get; set; }
public int? PTVID { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialAssignment> PTCredentialAssignments { get; set; }
public virtual PTVendor PTVendor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialContactAssignment> PTCredentialContactAssignments { get; set; }
}
public partial class PTVendor
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTVendor()
{
PTConnections = new HashSet<PTConnection>();
PTCredentials = new HashSet<PTCredential>();
PTIneligableCredentialVendorAssignments = new HashSet<PTIneligableCredentialVendorAssignment>();
PTVendorAssignments = new HashSet<PTVendorAssignment>();
PTVendorContactAssignments = new HashSet<PTVendorContactAssignment>();
}
[Key]
public int PTVID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50), Display(Name="Vendor Name")]
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnection> PTConnections { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredential> PTCredentials { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTIneligableCredentialVendorAssignment> PTIneligableCredentialVendorAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTVendorAssignment> PTVendorAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTVendorContactAssignment> PTVendorContactAssignments { get; set; }
}
[Table("PTCredentialAssignment")]
public partial class PTCredentialAssignment
{
[Key]
public int PTCRAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
public int PTCRID { get; set; }
public virtual PTConnection PTConnection { get; set; }
public virtual PTCredential PTCredential { get; set; }
}
[Table("PTConnectionClass")]
public partial class PTConnectionClass
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTConnectionClass()
{
PTConnections = new HashSet<PTConnection>();
}
[Key]
public int PTCCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50)]
public string Class { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnection> PTConnections { get; set; }
}
[Table("PTConnectionContactAssignment")]
public partial class PTConnectionContactAssignment
{
[Key]
public int PTCCAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
[Required]
[StringLength(60)]
public string FirstName { get; set; }
[Required]
[StringLength(60)]
public string LastName { get; set; }
[Required]
[StringLength(10)]
public string PhoneNumber { get; set; }
[StringLength(20)]
public string Extension { get; set; }
[StringLength(100)]
public string Email { get; set; }
[StringLength(60)]
public string Title { get; set; }
public string Comment { get; set; }
public virtual PTConnection PTConnection { get; set; }
}
Here is my ViewModel:
public partial class PTConnectionViewModel
{
[Key]
public int PTCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(255)]
public string ConnectionName { get; set; }
[Required]
[StringLength(100)]
public string Host { get; set; }
public int Port { get; set; }
public string Comment { get; set; }
public int PTCCID { get; set; }
public bool? IsDisabled { get; set; }
public int PTVID { get; set; }
public virtual PTConnectionClassViewModel PTConnectionClass { get; set; }
public virtual ICollection<PTConnectionAssignmentViewModel> PTConnectionAssignments { get; set; }
public virtual ICollection<PTCredentialAssignmentViewModel> PTCredentialAssignments { get; set; }
public virtual PTVendorViewModel PTVendor { get; set; }
public virtual ICollection<PTConnectionContactAssignmentViewModel> PTConnectionContactAssignments { get; set; }
}
public partial class PTVendorViewModel
{
[Key]
public int PTVID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50), Display(Name="Vendor Name")]
public string Name { get; set; }
public virtual ICollection<PTConnectionViewModel> PTConnections { get; set; }
public virtual ICollection<PTCredentialViewModel> PTCredentials { get; set; }
}
public partial class PTCredentialViewModel
{
[Key]
public int PTCRID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(100)]
public string CredUsername { get; set; }
[Required]
[StringLength(500)]
public string CredPassword { get; set; }
public string Directory { get; set; }
public bool? IsDisabled { get; set; }
public string Comments { get; set; }
public int? PTVID { get; set; }
public virtual ICollection<PTCredentialAssignmentViewModel> PTCredentialAssignments { get; set; }
public virtual PTVendorViewModel PTVendor { get; set; }
public virtual ICollection<PTCredentialContactAssignmentViewModel> PTCredentialContactAssignments { get; set; }
}
public partial class PTCredentialAssignmentViewModel
{
[Key]
public int PTCRAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
public int PTCRID { get; set; }
public virtual PTConnectionViewModel PTConnection { get; set; }
public virtual PTCredentialViewModel PTCredential { get; set; }
}
public partial class PTConnectionClassViewModel
{
[Key]
public int PTCCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50)]
public string Class { get; set; }
public virtual ICollection<PTConnectionViewModel> PTConnections { get; set; }
}
public partial class PTConnectionContactAssignmentViewModel
{
[Key]
public int PTCCAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
[Required]
[StringLength(60)]
public string FirstName { get; set; }
[Required]
[StringLength(60)]
public string LastName { get; set; }
[Required]
[StringLength(10)]
public string PhoneNumber { get; set; }
[StringLength(20)]
public string Extension { get; set; }
[StringLength(100)]
public string Email { get; set; }
[StringLength(60)]
public string Title { get; set; }
public string Comment { get; set; }
public virtual PTConnectionViewModel PTConnection { get; set; }
}
Here is the mapping that made my solution work:
cfg.CreateMap<PTConnection, PTConnectionViewModel>()
.AfterMap((s, d) => { foreach (var i in d.PTCredentialAssignments) i.PTConnection = d; });
cfg.CreateMap<PTCredentialAssignment, PTCredentialAssignmentViewModel>()
.ForMember(m => m.PTConnection, opt => opt.Ignore());
cfg.CreateMap<PTVendor, PTVendorViewModel>()
.ForMember(m => m.PTCredentials, opt => opt.Ignore())
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTCredential, PTCredentialViewModel>()
.ForMember(m => m.PTCredentialAssignments, opt => opt.Ignore())
.ForMember(m => m.PTCredentialContactAssignments, opt => opt.Ignore());
cfg.CreateMap<PTConnectionClass, PTConnectionClassViewModel>()
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTConnectionContactAssignment, PTConnectionContactAssignmentViewModel>()
.ForMember(m => m.PTConnection, opt => opt.Ignore());

how to get student courses with entity framework

I have 2 classes, course and student, and I want to get student courses with student id
help me please
I'm using entity framework code first
public class Course {
public int CourseID { get; set; }
public string Title { get; set; }
public string Details { get; set; }
public string img { get; set; }
public DateTime DateAdded { get; set; }
public Guid UserAddID { get; set; }
public Guid userEditID { get; set; }
public int SubCategoryID { get; set; }
public string Code { get; set; }
public string Summury { get; set; }
public decimal Price { get; set; }
public bool IsGift { get; set; }
public DateTime DateUpdated { get; set; }
public bool DelFlag { get; set; }
public DateTime? DelDate { get; set; }
public Guid? UserDelID { get; set; }
public ICollection<Student> Students { get; set; }
}
public class Student {
[Key]
public int StudentID { get; set; }
public string Name { get; set; }
public DateTime? DateBirth { get; set; }
public int? Age { get; set; }
public int? Sex { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Mobile2 { get; set; }
public string UniversityID { get; set; }
public string CollegeID { get; set; }
public string IdentityID { get; set; }
public string Address { get; set; }
public string Education { get; set; }
public decimal? Payments { get; set; }
public string HowToKnow { get; set; }
public DateTime? GradeDate { get; set; }
public string LaterCourses { get; set; }
public string Job { get; set; }
public string Specialist { get; set; }
public string Notes { get; set; }
public int StatueID { get; set; }
public Guid? UserID { get; set; }
public DateTime? DateAdded { get; set; }
public Guid UserAddID { get; set; }
public Guid userEditID { get; set; }
public DateTime DateUpdated { get; set; }
public bool DelFlag { get; set; }
public DateTime? DelDate { get; set; }
public Guid? UserDelID { get; set; }
public virtual IList<Course> Courses { get; set; }
}
I want to get all courses for a given StudentID using a lambda expression.
var students = repository.Query<Course>(c => c.Student.Any(st=>st.StudentID == StudentID)

EntityFramework get related item by id or field

I have two objects like:
public class Area : IEntity
{
public virtual int Id { get; set; }
public virtual int AreaTypeLookupId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string Name { get; set; }
[Column(TypeName = "varchar")]
[StringLength(2)]
public virtual string VMRegionCode { get; set; }
public virtual int GISPrimaryKeyId { get; set; }
public virtual string ConcatenatedGISAreaIds { get; set; }
public virtual int? ParentAreaId { get; set; }
public virtual int? ParentRegionAreaId { get; set; }
[Column(TypeName = "char")]
[StringLength(3)]
public virtual string CostCenterCode { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string CreatedByUserName { get; set; }
public DateTime CreatedDateTime { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string LastModifiedByUserName { get; set; }
public DateTime? LastModifiedDateTime { get; set; }
// Navigation properties
public virtual AreaTypeLookup AreaTypeLookup { get; set; }
}
public class AreaTypeLookup : IEntity
{
[Column("Id")]
public virtual int AreaTypeLookupId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string Name { get; set; }
[Column(TypeName = "varchar")]
[StringLength(10)]
public virtual string ShortName { get; set; }
public virtual int? AreaTypeLevelLookupId { get; set; }
public virtual int? OperationTypeLookupId { get; set; }
public virtual int? ParentAreaTypeLookupId { get; set; }
public bool IsAvailableToSetBudgetAgainst { get; set; }
public int DisplaySortOrder { get; set; }
public bool IsProtected { get; set; }
public DateTime? DeletedDateTime { get; set; }
public int? ManagedByServiceTypeLookupId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string CreatedByUserName { get; set; }
public DateTime CreatedDateTime { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string LastModifiedByUserName { get; set; }
public DateTime? LastModifiedDateTime { get; set; }
}
What I want is to get all the areas where areatypelookupid = 3. For this area type the name is "region"
What is the best way to get the areas for code maintainability:
This:
var areas = _unitOfWork.AreaRepository.GetAll().Where(x => x.AreaTypeLookupId == 3);
or this:
var areas = _unitOfWork.AreaRepository.GetAll().Where(x => x.AreaTypeLookup.Name == "region");
where I guess "region" should really becoming from a resources file.
Typically, you will want to use the primary key as that should be immutable. However, if you can guarantee that the lookup name is not going to change, then searching by name would be tolerable. The reason I say tolerable is because running queries off of the primary key will be more efficient on the database, on top of the immutability.