I have 2 entities being them: Employees and SendMessage:
public class Employee
{
[DbColumn(IsIdentity =true, IsPrimary =true)]
public long EmployeeId { get; set; }
[DbColumn]
public string Name { get; set; }
[DbColumn]
public string Surname { get; set; }
[DbColumn]
public string Date_Birth { get; set; }
[DbColumn]
public string Home_Address { get; set; }
[DbColumn]
public string City { get; set; }
[DbColumn]
public string Postcode { get; set; }
[DbColumn]
public string Telephone { get; set; }
[DbColumn]
public string Mobile { get; set; }
[DbColumn]
public string Email { get; set; }
[DbColumn]
public long ShiftId { get; set; }
}
As you can see the EmployeeId field is to connect the two
public class MessageSent
{
[DbColumn(IsIdentity =true, IsPrimary =true)]
public long MessageSentId { get; set; }
[DbColumn]
public long EmployeeId { get; set; }
[DbColumn]
public long MessageSentSeq { get; set; }
[DbColumn]
public string Status { get; set; }
[DbColumn]
public string DateSent { get; set; }
}
To redeem I use the following method
gvEmployee.DataSource = new EmployeeService().GetAll();
Now comes my need I need to show on my new screen the following fields:
MessageSentId,EmployeeId,MessageSentSeq of the table MessageSent and Name,Surname of the table Employees.
How can I create a third list with these 5 fields to fill my grid?
Take your employees:
var employees = new EmployeeService().GetAll();
Then your messages:
var messages = new MessageSentService().GetAll(); // probably like this, idk what its in your code
Using the LINQ query syntax is much clearer, more natural, and makes it easier to spot errors:
var query =
from employee in employees
join message in messages
on employee.EmployeeId equals message.EmployeeId
select new {
MessageSentId = message.MessageId,
EmployeeId = message.EmployeeId,
MessageSentSeq = message.MessageSentSeq,
Name = employee.Name,
Surname = employee.Surname
};
Then you can use query and its fields.
Related
I am working on school project which is about showing some database properties that I learned.I have 2 entity classes called Person and CarInformation.I want to show Name, Location,WhishedDays from Person and Brand, PlateNumber from CarInformation,together.
I tried generating new class called then I grouped Name, Location, WishedDays,Brand,PlateNumber.In the corresponding controller, I made a List with Select function but failed.
CarInformation Entity Class
public int Id { get; set; } //Primary key of carinfo table
public string Brand{ get; set; }
public string PlateNumber { get; set; }
public int Milage { get; set; }
public string InsuranceCompany{ get; set; }
public double ConsumptionPerMile { get; set; }
public int DailyPrice { get; set; }
public DateTime AvailableDates { get; set; }
[ForeignKey("Persons")]
public int OwnerId { get; set; } //foregein key (Person table to carinfo table)
public virtual Person Persons { get; set; } //Navigation property
public List<Sales> Sales { get; set; }
Person Entity Class
public int Id { get; set; } //primary key of person table
public string Name { get; set; }
public string Location { get; set; }
public int WishedDays { get; set; }
public virtual CarInformation Cars { get; set; } //Navigation property
public List<CarInformation> cars { get; set; } //bir insana ait birden fazla araba olabilir
public List<Sales> sales { get; set; }
Person-CarInformation Class
public string Name { get; set; }
public string Location { get; set; }
public int WishedDays { get; set; }
public string Brand { get; set; }
public string PlateNumber { get; set; }
Corresponding controller`s Index Method
public ActionResult Index()
{
var isimler = db.People.
Select(i => new Person_CarInformation() { Location=i.Location,Name=i.Name, Brand=i.Cars.Brand,PlateNumber=i.Cars.PlateNumber});
return View(isimler.ToList());
}
When I execute in this circumstances I get this result
Brand and PlateNumber is empty.
Any advises?
Thank You.
https://imgur.com/D9a8whD
You haven't included dependent entity while fetching data. You can get it by changing your code like shown below.
var isimler = db.People.Include("Cars").
Select(i => new Person_CarInformation() { Location=i.Location,Name=i.Name, Brand=i.Cars.Brand,PlateNumber=i.Cars.PlateNumber});
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.
So I'm creating a new tool in C# and I've created a class called 'Customer' and each 'Customer' has sub-group of employees which is an array of names. How do I set this property in my class for 'Customer'? What I have below for 'Employees' is not correct. I just left it there as a placeholder.
Thank you
public class Customer
{
public String Number { get; set; }
public String Name { get; set; }
public String Street { get; set; }
public String City { get; set; }
public String State { get; set; }
public String Zipcode { get; set; }
public string[] Employees = { get; set; }
}
You could use a List instead of an array as it is easier to manipulate:
public class Customer
{
public String Number { get; set; }
public String Name { get; set; }
public String Street { get; set; }
public String City { get; set; }
public String State { get; set; }
public String Zipcode { get; set; }
public List<string> Employees { get; set; }
}
And then when you instantiate it, you could add new employers as such:
Customer customer = new Cusomter();
customer.Number = "num1";
customer.Name = "ABC";
//...
List<string> lstEmp = new List<string>();
lstEmp.Add("NewEmployee1");
lstEmp.Add("NewEmployee2");
customer.Employees = lstEmp;
And read it like this:
foreach (string name in customer.Employees)
{
Console.WriteLine(name);
}
Simply use this declaration:
public string[] Employees { get; set; }
Trying to map between a viewmodel and my model. In this scenario, i am getting taking data from a web service (creating a strongly typed view) and then loading it into a form. I then verify the client data from the web service, submit the form. It will do an insert or update depending on whether or not there's a record in my database.
I have been meaning to use AutoMapper for some time so this is my first experience with it. I want the properties from SearchResult to be mapped to my Client Model.
namespace Portal.ViewModels
{
public class ClientSearch
{
public SearchForm searchForm { get; set; }
public SearchResult searchResult { get; set; }
}
public class SearchForm
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string DOB { get; set; }
public string AccNumber { get; set; }
}
public class SearchResult
{
public int ClientID { get; set; }
public string AccNumber { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Gender { get; set; }
public string DOB { get; set; }
public int Age { get; set; }
}
}
namespace Portal.Models
{
public class Client
{
public int ClientID { get; set; }
public string AccNumber { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Gender { get; set; }
[Display(Name = "Date of Birth")]
public DateTime DOB { get; set; }
public int Age { get; set; }
}
}
In my controller below, i'm trying to use AutoMapper to map the data in clientSearch to my Client model based on an HttpPost.
However, I'm getting the error: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement when trying to create the Map. This is my attempt at using AutoMapper.
[HttpPost]
public ActionResult Process(ClientSearch clientSearch)
{
// Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Mapper.CreateMap<ClientSearch, Client>;
Client client = Mapper.Map<ClientSearch, Client>(clientSearch);
ClientRepository.InsertOrUpdate(client);
ClientRepository.Save();
}
You seem to be missing your '()' after:
Mapper.CreateMap<ClientSearch, Client>;
i.e. it should be:
Mapper.CreateMap<ClientSearch, Client>();
I am trying insert into database my model with relation one to many:
I have two models:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public virtual Country Country { get; set; }
}
public class Country
{
public int Id { get; set; }
public string CountryName { get; set; }
public virtual ICollection<User> Users { get; set; }
}
User user = new User()
{
Name = model.Name,
Surname = model.Surname,
Country = new Country { Id = 1 }
};
When I try to insert it to the database I get exception
Validation failed for one or more entities.
The CountryName field is required (CoutryName column is required in database)
I don't want each time call to database to get full Country object, any ideas?
Instead of instancing a new country, do something like this:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int CountryId { get; set; }
[ForeignKey("CountryId")]
public virtual Country Country { get; set; }
}
public class Country
{
public int Id { get; set; }
public string CountryName { get; set; }
public virtual ICollection<User> Users { get; set; }
}
User user = new User()
{
Name = model.Name,
Surname = model.Surname,
CountryId = 1;
};
This explicitly sets the CountryID on the User.
You should use "FK Associations" instead "Independent Associations" this is create article about it:
http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx