Elastic search Nest (.Net) Total and Documents don't match - c#

I'm having the following situation: the Total from the Valid Nest Response is equals 2 and the Documents count equal 0. I'm not sure how that is possible.

Found the solution. The problem was in my model. You see, my model was something like that:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string Nickname { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
public string MaritalStatus { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
And this Index had Documents missing some fields, like:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
}
The solution was adding the decorator PropertyName from Nest, to Map my model, like this:
public class Customer
{
[PropertyName("id")]
public string Id { get; set; }
[PropertyName("name")]
public string Name { get; set; }
[PropertyName("nickname")]
public string Nickname { get; set; }
[PropertyName("personType")]
public string PersonType { get; set; }
[PropertyName("gender")]
public string Gender { get; set; }
[PropertyName("maritalStatus")]
public string MaritalStatus { get; set; }
[PropertyName("birthDate")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
Now it works!

Related

How To Assign the value in OneModel to AnotherModel

How to Assign the value in one model to another model without using AutoMapper ,foreach,and instance and then it will access any way to send value in one model to another model its is possible and then my source model will be byte array ..
This is my source model...
public class OneDriveItem : OneDriveItemBase
{
public string Id { get; set; }
public string Name { get; set; }
public string ETag { get; set; }
public OneDriveIdentitySet CreatedBy { get; set; }
public DateTimeOffset CreatedDateTime { get; set; }
public OneDriveIdentitySet LastModifiedBy { get; set; }
public DateTimeOffset LastModifiedDateTime { get; set; }
public long Size { get; set; }
public string WebUrl { get; set; }
public OneDriveItem[] Children { get; set; }
public OneDriveRemoteItem RemoteItem{ get; set; }
}
This is my Target Model
public class OneDriveModels
{
public string Id { get; set; }
public string Etag { get; set; }
public string Name { get; set; }
public string WebUrl { get; set; }
public string CTag { get; set; }
public long Size { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime LastModifiedDateTime { get; set; }
public string DownloadUrlAnnotation { get; set; }
}
You can decide not to look for a shortcut and do it with a constructor/factory method.
For example:
public class OneDriveModels
{
public static FromOneDriveItem( OneDriveItem source)
{
Id = source.Id
ETag = source.ETag
(...)
}

Entity Framework - Update action and Automapper

For example, I have the following infrastructure class:
[Table("GeoHistory")]
public partial class GeoHistory
{
public int Id { get; set; }
public int CompanyId { get; set; }
public int DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
[StringLength(100)]
public string GeofenceLocation { get; set; }
[StringLength(100)]
public string GeofencePrevious { get; set; }
[StringLength(20)]
public string StateLocation { get; set; }
[StringLength(20)]
public string StatePrevious { get; set; }
public DateTime? DateTime { get; set; }
[StringLength(5)]
public string Heading { get; set; }
public decimal? Speed { get; set; }
[StringLength(50)]
public string Status { get; set; }
}
and the following View class (let's forget about domain layer):
public class GeoHistoryViewModel
{
public int Id { get; set; }
public int? CompanyId { get; set; }
public int? DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
}
as we can see, we edit only part of field list.
Now, we want to update data in DB. Of course, we can write like:
Infrastructure.Main.GeoHistory geoHistory = db.GeoHistories.Find(id);
geoHistory.CompanyId = model.CompanyId;
geoHistory.DriverId = model.DriverId;
geoHistory.Name = model.Name;
........
db.SaveChanges();
It works. But I want to use Automapper. And if I try to do the following:
Infrastructure.Main.GeoHistory geoHistory = mapper.Map<Infrastructure.Main.GeoHistory>(model);
db.GeoHistories.Attach(geoHistory);
db.Entry(geoHistory).State = EntityState.Modified;
db.SaveChanges();
It works, but of course remove values of fields, which are not exist in View Model, but exist in infrastructure class. How to use automapper, but don't lost these fields?

How to setup a relational data model?

I am completely new to ASP.NET and was trying to cross-reference two different models with each other. I have 'Customer':
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
and 'Reservation':
public class Reservation
{
public int ReservationID { get; set; }
public string PetName { get; set; }
public DateTime Birthdate { get; set; }
public string Specie { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Customer Customer { get; set; }
}
Now, each Reservation should belong to a Customer (as you can see in the bottom line of the Reservation model) and should therefore contain the CustomerID. On the other hand should each Customer contain references to each Reservation that was made by him.
How can I setup this relation?
Use an ICollection for a one to many relationship.
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
Note I originally suggested a generic list, but there's a few subtle differences.

Instantiate one class variable with another

I have a the following classes
namespace MyClass.Models
{
public class Address : IIdentity
{
public string id { get; set; }
public DateTime updatedAt { get; set; }
public string user_id { get; set; }
public int building_number { get; set; }
public string street { get; set; }
public string area { get; set; }
public string county { get; set; }
public string country { get; set; }
public string postcode { get; set; }
public bool iscustomer { get; set; }
public bool islive { get; set; }
public string customer_id { get; set; }
public string store_id { get; set; }
public string phone_id { get; set; }
}
}
and
namespace MyClass.Models
{
public class User : IIdentity
{
public string id { get; set; }
public DateTime updatedAt { get; set; }
public string username { get; set; }
public string password { get; set; }
public bool isrestricted { get; set; }
public bool isbanned { get; set; }
public DateTime renewaldate { get; set; }
public string address_id { get; set; }
public string userlevel { get; set; }
}
}
What I am trying to do is create some test data to propagate through to my database so I can test my MVC5 website.
I am attempting to perform a look up on my instance of User within the Address class like so
user_id = context.User.FirstOrDefault(t=>t.address_id == id)
where id points to the id within the class I and trying instantiate.
VS2013 is saying that id does not exist in the current context which makes sense. Is there a way to do what I'm trying to achieve or am I better off setting the my _id from within the class itself and will that be okay when I run update-database from within the package manager console?
For example, the address class would become
namespace MyClass.Models
{
public class Address : IIdentity
{
public string id { get; set; }
public DateTime updatedAt { get; set; }
public string user_id { get {user_id = Users.FirstOrDefault(t=>t.address_id == id).id; } }
public int building_number { get; set; }
public string street { get; set; }
public string area { get; set; }
public string county { get; set; }
public string country { get; set; }
public string postcode { get; set; }
public bool iscustomer { get; set; }
public bool islive { get; set; }
public string customer_id { get {customer_id = Customers...;} }
public string store_id { get {store_id = Stores...;} }
public string phone_id { get {phone_id = Phones...;} }
}
}

mvc custom validation min price

I have a project. And I need a custom validation for find minimum price. Now first off all my model is in here;
public class ReservationModel
{
[Required]
public DateTime BeginDate { get; set; }
[Required]
public DateTime EndDate { get; set; }
[Required]
public double Price { get; set; }
[Required]
public string EmailAddress { get; set; }
public Guid ApartId { get; set; }
[Required]
public int Guest { get; set; }
public string ApartName { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string ApartDesc { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string CategoryName { get; set; }
public double MinAmount { get; set; }
public string ApartImage { get; set; }
public string CheckInTime { get; set; }
public string CheckOutTime { get; set; }
public int DayOfReservation { get; set; }
public int ReservationGuestNumber { get; set; }
}
I want to use this validation in MinAmount. I have dynamic prices. For example apart price 300 USD. I want to MinAmount Value is must be 300 X 25 /100 = 75 USD. Validation error fire is If client write 50 USD or under 75 USD. So How can create My custom MinPriceAttribute??? Thanks for kindly replies.
What you can do is create your custom filter.
You can find some tips here mvc custom filter
Video tutorial

Categories