Excluding dates from Linq Query in MVC .net application - c#

Within an asp.net MVC app, I'm trying to find rooms which have a guest/client, where the guest is leaving before a certain date.
The Client model class has foreign key, RoomId:
public class Room
{
public int RoomId { get; set; }
[Display(Name = "Room Name")]
public string Name { get; set; }
public bool Disabled { get; set; }
public List<Client> Clients { get; set; }
}
public class Client
{
public int ClientId { get; set; }
public int RoomId { get; set; }
public string RoomName { get; set; }
public DateTime Arrival { get; set; }
public DateTime Departure { get; set; }
public Room Room { get; set; }
}
My current Linq query is:
from r in Rooms
where r.Disabled == false
//where r.Clients.Departure<=DateTime.Parse("2012-07-01")
select new
{
r.Name,
r.Disabled
}
The commented line: //where r.Clients.Departure..... brings up the following error in LinqPad:
'System.Data.Linq.EntitySet' does not contain a
definition for 'Departure' and no extension method 'Departure'
accepting a first argument of type
'System.Data.Linq.EntitySet' could be found
(press F4 to add a using directive or assembly reference)
Is there any way, within Linq, that I can run this query, to exclude the Departure date where clause?
Thanks for any help,
Mark

After your comment, this one should do what you need
&& r.Clients.All(client => client.Departure<=DateTime.Parse("2012-07-01"))
Edit :
Maybe declare the DateTime to compare out of the query
var dt = DateTime.Parse("2012-07-01");
and
&& r.Clients.All(client => client.Departure<=dt)

What happens if you change the Room reference in Client to be virtual and the Client reference in Room to be a virtual ICollection? That should help the EF to make the connection.
See here: http://msdn.microsoft.com/en-us/library/gg715120(v=vs.103).aspx or here for something from Julia Lerman on the subject: http://msdn.microsoft.com/en-us/data/hh134698.aspx
This should enable you to re-add the commented-out clause and it should work.
EDIT: Actually the commented out bit should change in line with Raphaël Althaus's correction.
I reckon a combination of what I and Raphaël have suggested is the best approach.
Like this
public class Room
{
public int RoomId { get; set; }
[Display(Name = "Room Name")]
public string Name { get; set; }
public bool Disabled { get; set; }
public virtual ICollection<Client> Clients { get; set; }
}
public class Client
{
public int ClientId { get; set; }
public int RoomId { get; set; }
public string RoomName { get; set; }
public DateTime Arrival { get; set; }
public DateTime Departure { get; set; }
public virtual Room Room { get; set; }
}

Related

EF Core - Save partial only if it does not exist

I have a model which has quite a few references governed by some foreign keys. I receive the entity as a JSON payload:
{
"Person": {
"Name": "John",
"Age": 24,
"Email": "john#contoso.com"
},
"RandomText": "This is my text"
}
Lets imagine this maps to a model which looks like this:
class Message {
public Guid PersonId { get; set; }
public Person Person { get; set; } // contains guid / name / age / email
public string Text { get; set; }
}
With an equivalent two tables, one for the person and one for the Message which has a reference to the person. These two are obviously 1 (Person) to many (Messages)
This illustrates my setup.
In order to automatically sync this I want to do some validations / checks on the data I receive, e.g: If the person exists, then attach that persons Guid, if the person does not exist, generate a Guid, save the person, then save the text.
My model is a little more complex, but follows the same pattern:
class Meeting {
public Guid LocationId { get; set; }
public Location Location { get; set; }
public string Subject { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public DateTime? BookedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public bool WasCancelled { get; set; }
public bool WasNoShow { get; set; }
public bool WasAdhoc { get; set; }
public virtual ICollection<Extended> Extended { get; set; }
public bool WasEnded { get; set; }
public DateTime? EndedAt { get; set; }
public virtual ICollection<Attendee> Attendees { get; set; }
public virtual ICollection<MeetingPurpose> Purpose { get; set; }
public virtual ICollection<MeetingRoomMeeting> MeetingRooms { get; set; }
public Guid? DeliveryId { get; set; }
public virtual Delivery Delivery { get; set; }
}
MeetingRoomMeeting and MeetingPurpose are join tables which merely maps a many to many relationship:
public Guid MeetingRoomId { get; set; }
public virtual MeetingRoom MeetingRoom { get; set; }
public Guid MeetingId { get; set; }
public virtual Meeting Meeting { get; set; }
Again the point is, I have some data which may have been saved once already - I need to ensure if this is the case, and not save it again. Is there any pattern to this, or is it simplest to do it with raw queries, or maybe rip apart the class "Meeting" from the component classes and validate / save them individually?

ASP.NET MVC multiple models in one view

I have the following models
Patient Class:
public class Patient
{
public int PatientID { get; set; }
public virtual Salutation salutation { get; set; }
public int SalutationID { get; set; }
public string Surname { get; set; }
public string Firstname { get; set; }
[Display(Name = "Date of Birth")]
[DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
public DateTime DOB { get; set; }
public string RegNo { get; set; }
public DateTime RegDate { get; set; }
public string Occupation { get; set; }
}
VatalSigns Class
public class VitalSign
{
public int VitalSignID { get; set; }
public string Sign { get; set; }
[Display(Name = "Lower Limit")]
public int? LowerHold { get; set; }
[Display(Name = "Upper Limit")]
public int? UpperHold { get; set; }
[Display(Name = "Unit Of Measurment")]
public string Units { get; set; }
}
PV class that stores VitalSigns for each patient
public class PVSign
{
public long PVSignId { get; set; }
[Display(Name = "Patient")]
public int PatientID { get; set; }
public VitalSign VitalSigns { get; set; }
//public IList<VitalSign> VitalSigns { get; set; }
public Patient patient { get; set; }
}
Now the problem I have is that I have not been able to get display on one from to enter the details. I want to select the Patient and the different Vitals signs will appear and I will save the ones I need to the PVSign table.
I have tired all sorts of samples from the web. You can see from the code below, this is the index stub:
public ActionResult Index()
{
var pVSigns = db.PVSigns.Include(p => p.patient).Include(p => p.PVSignId).Include(p => p.VitalSigns);
//var pVSigns = from o in db.Patients join o2 in db.PVSigns
//List<object> myModel = new List<object>();
//myModel.Add(db.Patients.ToList());
//myModel.Add(db.VitalSigns.ToList());
//return View(myModel);
return View(pVSigns.ToList());
}
How to I solve this issue. I am new to MVC, if it was Webforms, I would have been through with this project.
Thank you.
There is no single answer(solution) to this(your) problem. It depends on how you want to design/build/achieve your solution.
The simple, brute solution : Just have a view model as a wraper that has as his properties the classes(models) you made and work around that,
public class FullPatientDetailsViewModel
{
public Patient { get; set;}
public List<PVSign> PatientPvSigns { get; set;} // Patien PV Signs
}
Or use just a PatientViewModel and load his PVSigns async with Ajax.
There is no simple best solution, it all depends about what do you want to achieve as a bigger picture.
I've answered a similar question here. As Alexandru mentioned, the easiest and most straightforward way would be to wrap your models inside a ViewModel. You can read about other ways to implement this here.

Expanding classes using AutoMap

I have to import a set of data from one database to another with a somewhat different schema, and I'm considering using AutoMap. I could just write a bunch of SQL scripts, but I already have both databases in EF and I want to learn AutoMap ...
While many of the classes are similar, the problem I'm having is where the structure is really different. The target models were designed with several more layers of classes. Instead of flattening, I need to expand.
The target classes have the following properties:
public class Account
{
public int Id { get; set; }
public string Name { get; set; }
public ContactInfo Location { get; set; }
public List<Policy> Policies { get; set; }
}
public class ContactInfo
{
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public State State { get; set; }
public string Zip { get; set; }
public string EMail { get; set; }
public List<Phone> PhoneNumbers { get; set; }
}
public class Phone
{
public int Id { get; set; }
public string Name { get; set; }
public string Number { get; set; }
}
public class Policy
{
public int Id { get; set; }
public PolicyNumber PolicyNumber { get; set; }
public List<Transaction> Transactions { get; set; }
}
The source tables, however, are relatively flattened.
public partial class Account
{
public string AccountId { get; set; }
public string AccountName { get; set; }
public string PolicyNumber { get; set; }
}
public partial class Transaction
{
public int TransactionId { get; set; }
public int AccountId { get; set; }
public Nullable<System.DateTime> EffectiveDate { get; set; }
public string InsuredName { get; set; }
public string InsuredAddress { get; set; }
public string InsuredCity { get; set; }
public string InsuredState { get; set; }
public string InsuredZip { get; set; }
public string InsuredPhone { get; set; }
}
I can create the Map, but I don't know how to tell AutoMapper to handle converting the string Policy to a policy object and then add it to the list of Policies.
Mapper.CreateMap<Source.Account, Destination.Account>();
Even worse, the source data inexplicitly has the name and address info at the transaction level. Before you tell me that AutoMap might not be the best solution, please understand that these two source tables are 2 out of over 40 tables in this database, and that the others are not nearly as troublesome.
Can I configure AutoMap to convert the string property PolicyNumber to a Policy Object and add it to the Policies List of the target class?
Any suggestions on how I can get the name and address information from the Transaction into a ContactInfo class and add it at the Account level?
Thank you.
Thanks to Thomas Weller. Custom Value Resolvers handled exactly what I needed.

ASP.NET MVC /Entity Framework Error - Invalid column name 'Environment_Id'

I'm new to ASP.NET MVC and EF hopefully this is not a silly question
When i pass model to view i'm getting this error - Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'Environment_Id'.
Model nor database table has a property by that name. Could any guide me on this?.
**Here is the Version Model Class**
public partial class Version
{
public Version()
{
this.ProfileVersions = new List<ProfileVersion>();
this.ServerInfoes = new List<ServerInfo>();
}
public int Id { get; set; }
public string Number { get; set; }
public string Tag { get; set; }
public string Owner { get; set; }
public string Approver { get; set; }
public string Description { get; set; }
public virtual ICollection<ProfileVersion> ProfileVersions { get; set; }
public virtual ICollection<ServerInfo> ServerInfoes { get; set; }
}
**Profile Version Class**
public partial class ProfileVersion
{
public ProfileVersion()
{
this.PlatformConfigurations = new List<PlatformConfiguration>();
}
public int Id { get; set; }
public int ProfileId { get; set; }
public int EnvironmentId { get; set; }
public int VersionId { get; set; }
public Nullable<bool> Locked { get; set; }
public string LockedBy { get; set; }
public string Comments { get; set; }
public Nullable<int> Active { get; set; }
public virtual Environment Environment { get; set; }
public virtual ICollection<PlatformConfiguration> PlatformConfigurations { get;
set; }
public virtual PlatformProfile PlatformProfile { get; set; }
public virtual Version Version { get; set; }
}
**ServerInfo**
public partial class ServerInfo
{
public ServerInfo()
{
this.PlatformConfigurations = new List<PlatformConfiguration>();
}
public int Id { get; set; }
public string ServerName { get; set; }
public int ProfileId { get; set; }
public int VersionId { get; set; }
public int EnvironmentId { get; set; }
public string ServerType { get; set; }
public Nullable<short> Active { get; set; }
public string Domain { get; set; }
public string Location { get; set; }
public string IP { get; set; }
public string Subnet { get; set; }
public string Gateway { get; set; }
public Nullable<int> VLan { get; set; }
public string DNS { get; set; }
public string OS { get; set; }
public string OSVersion { get; set; }
public string Func { get; set; }
public Nullable<short> IISInstalled { get; set; }
public string ADDomainController { get; set; }
public string ADOrganizationalUnit { get; set; }
public string ADGroups { get; set; }
public string LastError { get; set; }
public Nullable<System.DateTime> LastUpdate { get; set; }
public virtual Environment Environment { get; set; }
public virtual ICollection<PlatformConfiguration> PlatformConfigurations { get;
set; }
public virtual PlatformProfile PlatformProfile { get; set; }
public virtual Version Version { get; set; }
public virtual VMConfiguration VMConfiguration { get; set; }
}
**Controller Code-**
public ViewResult Index(string id )
{
var profileVerList = from ver in _context.Versions
where !(from pfv in _context.ProfileVersions
select pfv.VersionId).Contains(ver.Id)
select ver;
var bigView = new BigViewModel
{
VersionModel = profileVerList.ToList(),
};
return View(model: bigView);
}
**In the View where the exception is thrown**
#Html.DropDownList(
"SelectedVersionID",
new SelectList(
Model.VersionModel.Select(x => new { Value = x.Id, Text = x.Number}),
"Value",
"Text"
)
)
In your ProfileVersion and ServerInfo entities you have an Environment navigation property. By default, Entity Framework will try to create a database column called [Property Name]_[Referenced class PK]. In your scenario, that's Environment_Id. The problem, right now, is that you have not done a migration to have this database column created.
If I had to imagine what happened here, I'd say you first created the classes with EnvironmentId properties, migrated, then later decided to add the navigation properties, Environment to each, expecting EF to associate that with your existing EnvironmentId properties. That's where you went wrong. As I said above, EF convention is to look for a database column named Environment_Id, so if you want EF to use EnvironmentId instead, you just need to tell it so with the ForeignKey data annotation:
[ForeignKey("Environment")]
public int EnvironmentId { get; set; }
In My Case I have added My Primary Key Relationship to Same Key .. SO I have simply remove..
I realize this question is 3 years old now, but I saw a different reason for the error - both in the original question and in my own code that was pretty similar. And, in my case, I had the same error as stated above.
I had a "MY_ACTIONS" table with an ID and Name pair that I wanted to be added to a dropdown. Here's the model:
namespace TestSite.Models
{
public class MY_ACTIONS
{
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public MY_ACTIONS()
{
this.o_actions = new HashSet<MY_ACTIONS>();
}
[Key]
public int action_id { get; set; }
[StringLength(100)]
public string action_name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MY_ACTIONS> o_actions { get; set; }
}
}
And to get an action to display on the dropdown it had an ID set in an int field called LASTACTION in my main table. In that model I had declared the ForeignKey relationship:
namespace TestSite.Models
{
[Table("MAIN_TABLE")]
public partial class MAIN_TABLE
{
[Key]
public int MAIN_TABLE_ID { get; set; }
public int LASTACTION { get; set; } // this would carry a number matching action_id
[ForeignKey("LASTACTION")]
public virtual MY_ACTIONS MY_ACTIONS { get; set; }
}
}
I had the error Invalid column name 'MY_ACTIONS_action_id' when loading this dropdown in my view:
#Html.DropDownList("lastaction", null, htmlAttributes: new { #class = "form-control" })
...for which I was using this ViewBag in my Controller function:
Model1 db = new Model1(); // database context
MAIN_TABLE o_main = new MAIN_TABLE();
o_main.lastaction = 2;
ViewBag.lastaction = new SelectList(db.MY_ACTIONS, "action_id", "action_name", o_main.lastaction);
If I did not have my FK relationship declared:
[ForeignKey("LASTACTION")]
public virtual MY_ACTIONS MY_ACTIONS { get; set; }
I probably also would've had the same issue. Having the representation of a virtual instance requires linking it with some physical property. This is similar to how this:
public virtual Environment Environment { get; set; }
Should be:
[ForeignKey("EnvironmentId")]
public virtual Environment Environment { get; set; }
in the ProfileVersion class, in the question, above, assuming that EnvironmentId is the Primary Key in a table called Environment (that model is not shown above).
For me, though, I already had that and I was still getting the error, so doing that still might not solve everything.
Turns out all I had to do was get rid of that ICollection<MY_ACTIONS> o_actions in the MY_ACTIONS model and the this.o_actions = new HashSet<MY_ACTIONS>(); line and it all went through fine.
There are many such lists and ICollections in play in the question above, so I would wager something is wrong with having them, as well. Start with just a plain model that represents the fields, then add in your virtual objects that represent tables linked to with foreign keys. Then you make sure your dropdown loads. Only after that should you start adding in your ICollections, HashSets, Lists<T> and other such amenities that are not actually physically part of the database - this can throw off Entity Framework into thinking it needs to do something with them that it doesn't need to do.

Querying the database using EF Code First and Linq

I've spent a good deal of time and can't figure out how to make this query work. I am making a hours of operation type module where the users can choose something like the following:
Monday open from 8am to 11am closed from 11am to 1pm and open from 1pm to 5pm
This is completely dynamic and the users can choose how many opens and close they want (dynamically generated form inputs)
To do this I have made a couple of classes (I have no idea if this is a proper way of doing this because I've been using asp.net mvc, C# and EF for about a week now. Any suggestions would be greatly appreciated)
public class HoursOfOperation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public IQueryable<CompanyHour> Monday { get; set; }
public IQueryable<CompanyHour> Tuesday { get; set; }
public IQueryable<CompanyHour> Wednesday { get; set; }
public IQueryable<CompanyHour> Thursday { get; set; }
public IQueryable<CompanyHour> Friday { get; set; }
public IQueryable<CompanyHour> Saturday { get; set; }
public IQueryable<CompanyHour> Sunday { get; set; }
//Navigation Property
public CompanyInformation Company { get; set; }
}
and
public class CompanyHour
{
public int id { get; set; }
public Status Status { get; set; }
public string From { get; set; }
public string To { get; set; }
}
public enum Status
{
Closed,
Open,
ByAppointmentsOnly
}
for completeness here is my companyInformation class
public class CompanyInformation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
//[Required]
[DisplayName("Company Name:")]
public string companyName { get; set; }
[DisplayName("Website Address:")]
[Url(ErrorMessage="The Website field is not a valid fully-qualified http, https, or ftp URL. (Example: http://www.website.com)")]
public string website { get; set; }
public string contactTitle { get; set; }
//[Required]
[DisplayName("Contact First Name:")]
public string contactFirstName { get; set; }
//[Required]
[DisplayName("Contact Last Name:")]
public string contactLastName { get; set; }
//[Required]
[Phone]
[DisplayName("Phone Number:")]
public string contactPhoneNumber { get; set; }
[DisplayName("Address Display?")]
public bool displayAddress { get; set; }
[DisplayName("Phone Number?")]
public bool displayPhoneNumber { get; set; }
//[Required]
[DisplayName("Address 1:")]
public string address1 { get; set; }
[DisplayName("Address 2:")]
public string address2 { get; set; }
//[Required]
[DisplayName("City:")]
public string city { get; set; }
//[Required]
[DisplayName("State:")]
public string state { get; set; }
//[Required]
[DisplayName("Zip/Postal Code:")]
public string zipCode { get; set; }
[DisplayName("Search Engine?")]
public bool allowSearchEngines { get; set; }
//navigation Properties
public virtual ICollection<UserProfile> CompanyUsers { get; set; }
public virtual ICollection<HoursOfOperation> CompanyHours { get; set; }
}
The reason, I have hardcoded the monday, tue.... in the hoursofoperation class is to make it easy for mvc to map the dynamically generated fields (and that part works!!! :) )
Now I want to query the database and make a hours of operations model to send back to the view (on get) so I can bring back the saved information. I however can't figure out how to do that. I'm sending back a HoursOfOperation class.
the query I have currently:
var model = company.CompanyHours.Where(e => e.Company.id == company.id);
just returns a HoursOfOperation model with only the id (correct id) all the companyHours entities are all null.
Can anyone help me come up with the proper query?
I think you need to do this make use of InClude method to get the child enitities
context.Companies
.Include("CompanyHours")
.Where(e => e.Company.id == company.id) ;
Ok so I finally fixed this problem but the way I did it was completely restructure my models to a better and correct design.

Categories