How to extend entity class? - c#

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

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

Entity Framework one-to-zero with different tables

I am wondering if it is possible to make one table related to many.
This is what I got now (working one-to-zero-or-one relation between Report and ReportHeader):
public class Report
{
public int Id {get; set;}
public ReportHeader ReportHeader {get; set;}
[ForeignKey("ReportHeader")]
public int ReportHeaderId {get; set;}
}
public class ReportHeader
{
[Key, ForeignKey("Report")]
public int Id {get; set;}
public Report Report {get; set;}
}
At this point I want to add table named Style to Report BUT also to table ReportHeader. Thus, the relations would look like this:
Report
|--ReportHeader
| |-- Style
|
|-- Style
After that the classes should look like:
public class Report
{
public int Id {get; set;}
public ReportHeader ReportHeader {get; set;}
public Style Style {get; set;}
[ForeignKey("ReportHeader")]
public int ReportHeaderId {get; set;}
[ForeignKey("Style")]
public int StyleId {get; set;}
}
public class ReportHeader
{
[Key, ForeignKey("Report")]
public int Id {get; set;}
[ForeignKey("Style")]
public int StyleId {get; set;}
public Report Report {get; set;}
public Style Style {get; set;}
}
This is so much fun... until it comes to think about the Style class. At this point I have no idea how to design it. Is that even possible to make that class be in two relations with different tables?
public class Style
{
// ???
//[Key, ForeignKey("Report"), ForeignKey("ReportHeader")]
public int Id {get; set;}
public ReportHeader ReportHeader {get; set;}
public Report Report {get; set;}
}
At this case you should to mask your desired relation: one-to-one as many-to-one:
public BaseClass
{
//indeed, collection always will have zero or one items
public virtual ICollection<Style> styles {get; set;}
[NotMapped]
public Style style {
get { return styles.FirstOrDefault(); }
set { styles.Add(value); };
}
}
public class Report : BaseClass
{
//other stuff...
}
public class ReportHeader : BaseClass
{
//other stuff...
}
public class Style
{
public int Id {get; set;}
public virtual Report report {get; set;}
[Index(IsUnique = true)]//to ensure that relation is exactly one-to-one
public int? reportId {get; set;}
public virtual ReportHeader reportHeader {get; set;}
[Index(IsUnique = true)]//to ensure that relation is exactly one-to-one
public int? reportHeaderId {get; set;}
}

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

Code First Model Mapping

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]

Categories