Code First Model Mapping - c#

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]

Related

Set Multiple Value For One Property

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.

How to get records from table that depends on another tables in entity framework core?

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();

Should foreign Id properties be mapped from Model to Dto?

If I have the following model:
public class Customer
{
public int Id {get; set;}
public int CustomerTypeId {get; set;}
public virtual CustomerType {get; set;}
}
Should the Dto exclude foreign Id's to look like this:
public class CustomerDto
{
public int Id {get; set;}
public virtual CustomerType {get; set;}
}
And when using Graphdiff to update the object graph, will EF know that CustomerType maps to CustomerTypeId?
Yes, you need to use it but you can avoid virtual member declaration. If you use AutoMapper, then the mapping will be done automatically. So, your Dto will look like this:
public class CustomerDto
{
public int Id {get; set;}
public int CustomerTypeId {get; set;}
}
And the mapping:
Mapper.CreateMap<Customer, CustomerDto>();
Mapper.CreateMap<CustomerDto, Customer>();

How to pivot rows for an editable grid?

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:

How to extend entity class?

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;}
}

Categories