Better way to create database for a pricing system - database

I need to create a price table system so I am going to create these three tables in my database.
PricingTable (ID, Name, ServiceID, Style)
PricingTablePackages (ID, PricingTable_ID, Title, Price, PricePerTime, Info, Flag, Link)
PricingTablePackagesFeatures (ID, PricingTablePackages_ID, Feature, Value, MoreInfo)
Here one PriceTable can hold more then one PricingTablePackages and one PricingTablePackage can hold more then one PricingTablePackagesFeature.
Is any way to design a better model? In a single database Table ?
I am going to create a MVC3 Model for those table so what is the best way to do this kind of DB Table in a MVC3 Model?

I would use public virtual variables for 'lazy-loading' values when you need them using Entity Framework:
(variable types may be off depending on exactly what you want for each variable)
public class PricingTablePackages
{
public int ID { get; set; }
public int PricingTableID { get; set; }
public virtual PricingTable PricingTable { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public decimal PricePerTime { get; set; }
public string Info { get; set; }
public bool Flag { get; set; }
public string Link { get; set; }
}
public class PricingTablePackagesFeatures
{
public int ID { get; set; }
public int PricingTableID { get; set; }
public virtual PricingTable PricingTable { get; set; }
public string Feature { get; set; }
public string Value { get; set; }
public string MoreInfo { get; set; }
}
public class PricingTable
{
public int ID { get; set; }
public string Name { get; set; }
public int ServiceID { get; set; }
public virtual Service Service { get; set; } // if there is a Service class
public string Style { get; set; }
}

Related

Change API response Object name (in Model) for Datagrid binding

I'm using WPF and still relatively new.
I haven't yet made the jump to MVVM, still trying to wrap my head around it.
I currently have a small app which extracts data from 4 different sources (at different times) and then displays it on a datagrid.
The issue is that the data model from the responses differs and I want to be able to easily use the same datagrid.
EG.
Datagrid binds to a model [zephyrPatientData]
Firstname : binds to firstName
last name: binds to lastName
ONE of the extractions has:
Firstname = firstName;
lastName = SURNAME.
Therefore if I use this data and try and bind to the datagrid, the LASTNAME firle is not filled as that model doesn't have lastName, it has surname.
Below: the datamodel that is working with the datagrid.
public class zephyrPatientData
{
public int CustomerID { get; set; }
public int ClaimID { get; set; }
public string ChemistID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ClaimNumber { get; set; }
public bool concern { get; set; }
public int? InsCompID { get; set; }
public string InsCompName { get; set; }
public int? EmployerID { get; set; }
public string EmployerName { get; set; }
public string DateOfInjury { get; set; }
public string dateOfBirth { get; set; }
public string Address { get; set; }
public int? SuburbID { get; set; }
public string SuburbName { get; set; }
public string Postcode { get; set; }
public string custPhone { get; set; }
public int? claimTypeID { get; set; }
public string InvoiceTo { get; set; }
public string Comments { get; set; }
public string ClaimManager { get; set; }
public string Injury { get; set; }
public bool activeClaim { get; set; }
public string ClaimManagerEmail { get; set; }
public int? LawyerID { get; set; }
public string LawyerCompany { get; set; }
public double? outstandingScripts { get; set; }
public double? outstandingValue { get; set; }
public string dispenseConnected { get; set; }
public string dispenseID { get; set; }
}
Below: The Datamodel I want to work with the datagrid.
public class zDispensePatientData
{
public string id { get; set; }
public int clientNo { get; set; }
public string firstName { get; set; }
private string surname;
public string lastName
{
get
{
return surname;
}
set
{
surname = value;
}
}
public string title { get; set; }
public object licenseNo { get; set; }
public string sex { get; set; }
public object dateOfBirth { get; set; }
public object postalAddress { get; set; }
public object postalSuburb { get; set; }
public object postalState { get; set; }
public object postalPostCode { get; set; }
public string homeAddress { get; set; }
public string homeSuburb { get; set; }
public string homeState { get; set; }
public string homePostCode { get; set; }
public string phoneNumber { get; set; }
public string mobileNumber { get; set; }
public object faxNumber { get; set; }
public string email { get; set; }
public DateTime lastModified { get; set; }
public bool active { get; set; }
public bool deceased { get; set; }
}
As you can see...I TRIED to use the get;set; section of the [zDispensePatientData] however I have no idea how to use this correctly.
In reality in laman's terms I want it to read surname from the API response but then be accessible as lastName in for use in the Datagrid.
The reaons for this is that there will be another 4 [zDispensePatientData] (other names) and all will have different field names but in the end I need them to work in the same datagrid without changing the binding of every single datagrid column.
EDIT: Expanded the question:
So I appreciate the responses but I'm not exactly grasping how to get this done.
If I were to take away all the model fields and just leave... 3:
firstName
lastName
patientID
my task is to have 5 different models (from responses from API's) 'allocate' to my own model.
eg.
<my model>
public string id { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
<model API 1>
public string id { get; set; }
public string patientLastName { get; set; }
public string patientFirstName { get; set; }
<model API 2>
public string patientid { get; set; }
public string surname{ get; set; }
public string FIRSTNAME{ get; set; }
Any app will only ever have one connection ([dispenseType]).
So I guess what I need is checking the user dispenseType and then using the model from the API they are using.
Or is it possible to SET myModel based on the contents of the other models.
eg.
if
API1.firstName = null
myModel.firstName = api2.FIRSTNAME
My main issue is I don't know the concept or question to ask. If there is a pointer to the terminology of what I'm trying to achieve I'm happy to go out and learn it to implement it.
If the column in the DataGrid binds to a property named "LastName", you need to make sure that the type T in the IEnumerable<T> ItemsSource of the DataGrid has such a property.
So add one to the zDispensePatientData class like you have currently done:
public string LastName
{
get => surname;
set => surname = value;
}
Another option is to set or bind the ItemsSource to a an IEnumerable<ViewModel> where the ViewModel class has the properties that the DataGrid expects.
You would then translate each zephyrPatientData and/or zDispensePatientData object to a ViewModel, e.g.:
sourceCollection = zDispensePatientDatas
.Select(x => new ViewModel() { LastName = x.Surname } );
How to map properties of two different objects?
This question answers my question but was stated in a better way than I could

I want to make a one-to-one relationship between two columns in ASP.NET Core

I have reading and jobOrder class. I want to create a relationship between joborderId in the jobOrder class and jobOrderId in reading class.
public class JobOrder
{
[Key]
public int Id { get; set; }
public int JobOrderId { get; set; }
public DateTime StartDate { get; set; }
public Nullable<DateTime> EndDate { get; set; }
public string MachineCode { get; set; }
public decimal TotalLength { get; set; }
}
public class Reading
{
public int Id { get; set; }
public string MachineCode { get; set; }
public decimal Length { get; set; }
public bool status { get; set; }
public DateTime time { get; set; }
public int JobOrderId { get; set; }
public JobOrder JobOrder { get; set; }
}
The best way is to take a look at the documentation: https://learn.microsoft.com/en-us/ef/core/modeling/relationships#one-to-one
If you do it the way you described then EF will choose one of the entities to be the dependent based on its ability to detect a foreign key property. If the wrong entity is chosen as the dependent, you can use the Fluent API to correct this.
When configuring the relationship with the Fluent API, you use the HasOne and WithOne methods.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasOne(p => p.BlogImage)
.WithOne(i => i.Blog)
.HasForeignKey<BlogImage>(b => b.BlogForeignKey);
}
If you follow the code first naming conventions EF will automatically discover the Key, ForeignKey and Navigation properties :
public class JobOrder
{
// Primary key (can be JobOrderId as well)
public int Id { get; set; }
// other fields...
// Foreign key
public int ReadingId { get; set; }
// Navigation property
public Reading Reading { get; set; }
}
public class Reading
{
// Primary key (can be ReadingId as well)
public int Id { get; set; }
// other fields...
// Foreign key
public int JobOrderId { get; set; }
// Navigation property
public JobOrder JobOrder { get; set; }
}
If you have more than one key composite keys to create the relationship, you need to manually define the keys and foreign keys:
public class JobOrder
{
[Key]
[Column(Order=1)]
public int Id { get; set; }
[Key]
[Column(Order=2)]
public int JobOrderNo { get; set; }
// other fields...
// Foreign key
public int ReadingId { get; set; }
// Navigation property
public Reading Reading { get; set; }
}
public class Reading
{
public int Id { get; set; }
// other fields...
[ForeignKey("JobOrder")]
[Column(Order=1)]
public int JobOrderId { get; set; }
[ForeignKey("JobOrder")]
[Column(Order=2)]
public int JobOrderNo { get; set; }
// Navigation property
public JobOrder JobOrder { get; set; }
}
How about going like this, it will create 1 to 1 relationship between JobOrder and Reading
public class JobOrder
{
[Key]
public int Id { get; set; }
public int JobOrderId { get; set; }
public DateTime StartDate { get; set; }
public Nullable<DateTime> EndDate { get; set; }
public string MachineCode { get; set; }
public decimal TotalLength { get; set; }
public virtual Reading Reading { get; set; }
}
public class Reading
{
[Key]
[System.ComponentModel.DataAnnotations.Schema.ForeignKey("JobOrder")]
public int Id { get; set; }
public string MachineCode { get; set; }
public decimal Length { get; set; }
public bool status { get; set; }
public DateTime time { get; set; }
public JobOrder JobOrder { get; set; }
}

Update database schema to allow multiple users

I'm working on an ASP.net Web API application and would like to allow multiple users to have access to their own data without changing the database schema too much.
My tables look a little like this:
public class Asset
{
public string Name { get; set; }
[Required]
public int Id { get; set; }
public string AssetTag { get; set; }
public string Serial { get; set; }
public int Model { get; set; }
}
public class Organisation
{
[Required]
public int Id { get; set; }
[Required]
public int DefaultLocation { get; set; }
public location Location { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Contact { get; set; }
}
public class AssetModel
{
public string Name { get; set; }
[Required]
public int Id { get; set; }
public int CategoryId { get; set; }
public int ManufacturerId { get; set; }
public string ModelNumber { get; set; }
}
*fields omitted for brevity
Each user should be able to create their own assets / organisations / etc, but should not have access to any other users fields.
I'm yet to add authorization / authentication however I'm probably going to use token based auth as outlined here:
http://bitoftech.net/2014/06/09/angularjs-token-authentication-using-asp-net-web-api-2-owin-asp-net-identity/
Should this be as easy as tacking each users GUID onto each column and applying some logic? Or will I need to completely re-design the database?

Invalid object name 'dbo.customers1'

I'm getting the error "Invalid object name 'dbo.customers1'" on my view...
#foreach (var item in Model)
{
<tr>
<td>#item.orderid</td>
<td>#item.customer.firstname</td>
I have the ViewModel classes...
public class orders
{
[Key]
public int orderid { get; set; }
public System.DateTime createdate { get; set; }
public string createdby { get; set; }
public Nullable<int> statusid { get; set; }
public Nullable<System.DateTime> pickup { get; set; }
public Nullable<System.DateTime> dropoff { get; set; }
public Nullable<System.DateTime> scheduledout { get; set; }
public Nullable<System.DateTime> scheduledin { get; set; }
public bool instorepickup { get; set; }
public string paymenttype { get; set; }
public System.DateTime reservationstart { get; set; }
public System.DateTime reservationend { get; set; }
public bool morningpickup { get; set; }
public Nullable<int> customerid { get; set; }
public string notes { get; set; }
public string shippingtype { get; set; }
public Nullable<decimal> shippingestimate { get; set; }
public virtual customer customer { get; set; }
public virtual ICollection<invoice> invoices { get; set; }
public virtual orderstatuses orderstatuses { get; set; }
public virtual ICollection<serialorders> serialorders { get; set; }
}
and
public class customers
{
[Key]
public int customerid { get; set; }
public System.DateTime createdate { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
[Required]
public string billingaddress1 { get; set; }
public string billingaddress2 { get; set; }
[Required]
public string billingcity { get; set; }
public string billingstate { get; set; }
[Required]
public string billingzip { get; set; }
[Required]
public string billingcountry { get; set; }
public string phone { get; set; }
[Required]
public string email { get; set; }
[Required]
public string shippingaddress1 { get; set; }
public string shippingaddress2 { get; set; }
[Required]
public string shippingcity { get; set; }
public string shippingstate { get; set; }
[Required]
public string shippingzip { get; set; }
[Required]
public string shippingcountry { get; set; }
public bool goodstanding { get; set; }
public string userid { get; set; }
public Nullable<DateTime> insuranceexp { get; set; }
public virtual ICollection<invoice> invoices { get; set; }
public virtual ICollection<order> orders { get; set; }
}
this is my model...
And I have a data access layer...
public DbSet<tvc.viewModels.orders> orders { get; set; }
public DbSet<tvc.viewModels.customers> customers { get; set; }
The error is pretty straight-forward. There's no dbo.customers1 table in your database. As to why, there's really not enough information here to say, but the most likely causes are:
You're using Database First or Code First with an existing database, and your entities are out of sync with the database.
You're using Code First, don't have migrations enabled, and you've made a change to either your entities or your database. Again, out of sync.
You've specified an explicit table name, either via the Table attribute or fluent config, that doesn't exist. Either way, change either the explicit table name to what it should be or rename your database table so it matches.
There was an oversight in my ViewModel. I was pointing customer, which is the name of the database table and the model class, rather than the viewmodel class which is customers (with an s).

EF many-to-many

How to build these tables for EF?
First table :
public class model1
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Second table :
public class model2
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Table which has a connection of the first and second
and several fields :
public class model3
{
public int (id from model1){ get; set; }
public int (id from model2) { get; set; }
public int (id from model2){ get; set; }
public decimal Name{ get; set; }
public decimal Description{ get; set; }
}
I think that this may do the trick (provide the table structure you described)
public class model3
{
public virtual model1 Model1 { get; set; }
public virtual model2 Model2 { get; set; }
public virtual model2 SecondModel2 { get; set; }
// if you want the ID fields explicitly,
// EF will map the keys by property name,
// optionaly you can use the ForeignKeyAttribute
public int Model1Id { get; set; }
public int Model2Id { get; set; }
public int SecondModel2Id { get; set; }
public decimal Name{ get; set; }
public decimal Description{ get; set; }
}
But you may want to rethink the structure - the one you presented looks a bit suspicious - model1 and model2 have the same fields, so maybe they shouldn't be defined as separate classes. The model3 class also has some common fields, so maybe it would be worth extracting them into a common class - EF handles inheritance.
To get a many to many relation though, you need something like this:
public class Foo
{
public int Id {get;set;}
public virtual ICollection<Bar> Bars {get;set;}
}
public class Bar
{
public int Id {get;set;}
public virtual ICollection<Foo> Foos {get;set;}
}
this will create a Foo table, a bar table and a FooBars (or BarFoos) associacion table.

Resources