Database Reference
In-Depth Information
Solution
Let's say that you have a table describing experimental medical drugs. The table contains a column indicating when
the drug was accepted for production. Until the drug is accepted for production, it is considered experimental. Once
accepted, it is considered a medicine. We'll start with the Drug table in the database diagram in Figure 6-7 .
Figure 6-7. Drug table with the nullable discriminator column, AcceptedDate
To create a model using the Drug table, do the following:
1.
Create a class in your project that inherits from DbContext.
2.
Create Drug, Medicine, and Experimental POCO entity classes, as shown in Listing 6-17.
Listing 6-17. Creating the Drug, Medicine, and Experimental POCO Entity Classes
[Table("Drug", Schema = "Chapter6")]
public abstract class Drug
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DrugId { get; set; }
public string Name { get; set; }
}
public class Experimental : Drug
{
public string PrincipalResearcher { get; set; }
public void PromoteToMedicine(DateTime acceptedDate, decimal targetPrice,
string marketingName)
{
var drug = new Medicine { DrugId = this.DrugId };
using (var context = new DrugContext())
{
context.Drugs.Attach(drug);
drug.AcceptedDate = acceptedDate;
drug.TargetPrice = targetPrice;
drug.Name = marketingName;
context.SaveChanges();
}
}
}
 
Search WWH ::




Custom Search