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.
Related
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 an object called Product which have the attribute called ProductItem and tags.
public class Shop{
public string Id {get; set;}
public Product Products {get; set;}
}
public class Product{
public string Name {get; set;}
public string Id {get; set;}
public Tag Tags {get; set;}
public ProductItem ProductItems {get; set;}
public Shop ShopInformation {get; set;}
}
public class Tag{
public string Id {get; set;}
public string name {get; set;}
}
public class ProductItem{
public string Id {get; set;}
public Tag Tags {get; set;}
}
I would like to get all the tags in both Products and Tags from the datacontext
and I use this query
var result = from shop in dataContext.Shop
select new Response
{
Id = shop.Id,
Tags = (from product in dataContext.Product
where product.ShopInformation.Id == shop.Id
from productTag in
product.Tags.Union(product.ProductItems.SelectMany(s => s.Tags))
select productTag.Name).Distinct(),
};
However, the performance is pretty slow, and thus I would like to know if there is a way to collect all tags faster.
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;}
}