I have a requirement to be able to enter data in an editable grid, with dynamic columns determined by the number of rows in a lookup table. This isn't the actual example, but it's a similar, simple case of what I'm trying to do:
public class Student {
public long ID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
...
}
public class Assessment {
public long ID {get; set;}
...
}
public class AssessmentQuestion {
public long ID {get; set;}
public long AssessmentID {get; set;}
public int QuestionNo {get; set;}
public int PointValue {get; set;}
...
}
public class StudentAssessment {
public long ID {get; set;}
public long StudentID {get; set;}
public long AssessmentID {get; set;}
...
}
public class StudentAssessmentAnswer {
public long ID {get; set;}
public long StudentAssessmentID {get; set;}
public long AssessmentQuestionID {get; set;}
public int PointsAwarded {get; set;}
public string Comments {get; set;}
...
}
As per above structure, students can take a given assessment. We can take the creation of Assessment, AssessmentQuestion, Student and StudentAssessment data as given. Now the user is grading assessments, and she wants to go through all the test papers and enter the points awarded per student per question. So for maximum ease of use the grader should see an editable grid, with one student name per row, and columns for each question, and they could just tab through all the cells entering points and comments (if any) for each question. Then, on hitting "Save", the StudentAssessmentAnswer table should be populated. Of course, the read should be with an outer join, because even if there's no StudentAssessmentAnswer joining between the student assessment and the question, we still want to enter the points.
So, how do you make a grid like this, when the number of columns depends on the number of rows in the AssessmentQuestion table for this AssessmentID?
There are several commercially available grids that can do that (sort of), ie. Telerik MVC grid:
Related
I have two class that you can see below:
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
public decimal SuggestionPrice {get; set;}
public List<SaleOrder> SaleOrders {get; set;} = new List<SaleOrder>();
}
public class SaleOrder
{
public int Id {get; set;}
public List<Product> Products {get; set;} = new List<Product>();
}
as you can see,this two class have Many-To-Many RealationShip to eachother.in Product class i have SuggestionPrice property that i want save multiple value in it and it's difference for each SaleOrder Class.
for example,Someone offers $ 1,000 in one SaleOrder and someone else offers $ 2,000 in another SaleOrder for same product and infinitely another suggestions that I want to save them all in database.
problem is i don't know how can i do this?how can i know witch SuggestionPrice is for witch SaleOrder Class?
To save in database You should create a SuggestionPrice table like this:
public class SuggestionPrice
{
public int Id {get;set}
public int ProductId {get;set;}
public decimal Price {get;set;}
public Product Product {get;set;}
}
and change product class to this:
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<SuggestionPrice> SuggestionPrices {get; set;}
}
to create a one to many relation between Product and SuggestionPrice.
I want to get some records from Database that depends on three tables.
Three tables are:
1.Company(Id,Name)
2. Car(Id,CompanyId,Name)
3. Showroom(Id,CarId,Name)
Now a one company contains many cars and many cars may exist in many showrooms.
I want to get records from showroom table where company '2' cars exist along with cars. Is it possible to do it in entity framework core?
I think your entities will be like :
Company
public class Company
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<Car> Cars {get; set;}
}
Car:
public class Car
{
public int Id{get; set;}
public string Name {get; set;}
public int CompanyId{get; set;}
public Company Company {get; set;}
}
ShowRoom:
public class ShowRoom
{
public int Id{get; set;}
public string Name {get; set;}
public int CarId{get; set;}
public Car Car{get; set;}
}
In your method:
var context = new SomeContext();
var showRooms= context.ShowRooms
.Include(x=> x.Car)
.ThenInclude(x=> x.Company)
.Where(x=> x.Car.Company.Id== 2)
.ToList();
I have two tables that I need to join
employee
-id
-name
-department_id
and
department
-id
-department_name
How can I handle the joined data in a single class? Should I create properties for all the data for each table?
e.g
class employeerecord
{
private int id {get; set;}
private string name {get; set;}
private int department_id {get; set;}
private string department_name {get; set;}
}
Right now, I'm using a datatable for viewing joined tables.
The whole design should include all entities you have in your database, i.e. there's departament table, there should be corresponding class. Same applies to employee table. You should have following classes:
class Employee
{
public int id {get; set;}
public string name {get; set;}
public int department_id {get; set;}
public Departament department {get; set;}
}
class Departament
{
public int id {get; set;}
public string name {get; set;}
//public ICollection<Employee> Employees { get; set; }
}
When loading data, you should wirte join query, to populate both: Employee instance and Departament instance.
You can uncomment commented line, if you'd like to have list of employees in particular departament.
This solely depends on what you want to do with this classes. You can go either way.
If you want to display a single employee and that employee can only belong to one department, then nothing speaks against a flat model:
class employeerecord
{
private int id{get;set};
private string name{get;set;}
private int department_id{get;set;}
private string department_name{get;set;}
}
If an employee can be a member of multiple Departments, you are better off storing the departments in a collection:
public class EmployeeViewModel
{
public int id{get;set};
public string name{get;set;}
public IEnumerable<Deparment> Departments { get; set; }
}
The same goes for departments. A department will most likely consist of multiple employees:
public class DepartmentViewModel
{
public int id{get;set};
public string name {get;set;}
public IEnumerable<Employee> Employees { get; set; }
}
And there is no reason, why you can't do all at once and use the classes depending on your specific use case.
I have a similar structure like below.
public class Price
{
public decimal Amount {get; set;}
public string Currency {get; set;}
}
public class Product : BaseEntity
{
public int Id {get; set;}
public string Name {get; set;}
public Price Price {get; set;}
}
I want a Product table in database that will apart Price to its properties. For instance;
**ProductTable**
--Id
--Name
--Amount
--Currency
Then when I get a product from database, it will automatically bind Amount and Currency to Price object of the Product. How should I make this structure by using Entity Framework Code First ?
Price property have to be virtual, and it should work:
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
public virtual Price Price {get; set;}
}
EDIT:
You can specify custom column name by override OnModelCreating method in your DbContext class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().Property(u => u.Price.Amount)
.HasColumnName("Amount");
}
answer given by #MiĆosz Wierzbicki will create a seperate table for Price
but in your case i dont think you need a seprate table for that
so defining the Price as Complex type would same it inside the same Table with "Price" appended to database field names and will also automatically map the names when retriving or saving data
[ComplexType]
public class Price
{
public decimal Amount {get; set;}
public string Currency {get; set;}
}
Read more about [ComplexType]
I'm using Entity Framework and Mysql. And I have Entity classes for each tables.
But I do not know how to extend fields into entity class that does not defined in DB.
For example) I have test table and the table have id, price and qty fields.
My entity class is like this,
[Table('test')]
public class Test
{
[Key]
public int id {get; set;}
public decimal price {get; set;}
public int qty {get; set;}
}
Now, I need subtotal field in Test class. (for some reason, I can not modify DB)
so I try to make the Test class as Partial class,
[Table('test')]
public partial class Test
{
[Key]
public int id {get; set;}
public decimal price {get; set;}
public int qty {get; set;}
}
public partial class Test
{
public decimal? subTotal {get; set;}
}
Then I got an error : It say, 'Unknown column 'Extent1.subTotal' in 'field list''
Anybody know, How can I add the subTotal field into Test class without changing DB structure?
Please advice me.
Use the NotMappedAttribute for any properties you want to have in your model, but do not want Entity Framework to map to your database.
[Table('test')]
public class Test
{
[Key]
public int id {get; set;}
public decimal price {get; set;}
public int qty {get; set;}
[NotMapped]
public decimal? subTotal {get; set;}
}