Reputation: 1452
I'm working on an n-Teir project and this question is focusing on the Service (Buisness logic) and Data layers. I have in the data layer, three models: Aircraft, AircraftType, and AircraftLivery. Now I have a pretty good Idea of how to initialize the database with sample data, but I'm unsure of how to relate the different types. How would I create a livery and relate it to an aircraft, or the default livery of the aircraft type? How to I relate an aircraft type to an aircraft? Below is the current code which builds but I havn't ran yet. How would I implement the last class, Sample Data? Is there anything else in this code that you see that could be done differently?
In the Data Layer:
public abstract class ModelBase
{
[Key]
public int Id { get; set; }
public string LastUpdateUser { get; set; }
public DateTime LastUpdateDt { get; set; }
public bool IsDeleted { get; set; }
}
public class Aircraft : ModelBase
{
public Guid SerialNumber { get; set; }
public string Registration { get; set; }
public byte[] Image { get; set; }
[ForeignKey("Id")]
public AircraftType Type { get; set; }
[ForeignKey("Id")]
public AircraftLivery Livery { get; set; }
}
public class AircraftType : ModelBase
{
public string Manufacture { get; set; }
public string ICAO { get; set; }
public string Name { get; set; }
public bool IsTailDragger { get; set; }
public bool RetractableGear { get; set; }
public double StallSpeedFullFlaps { get; set; }
public double StallSpeedClean { get; set; }
public double CruiseSpeed { get; set; }
public double MinimumDragVelocity { get; set; }
public int LowerThrottleLimit { get; set; }
public int MaximumMachSpeed { get; set; }
public int Range { get; set; }
public int Weight { get; set; }
public int Cruise { get; set; }
public int MaximumPassengers { get; set; }
[ForeignKey("Id")]
public ICollection<AircraftLivery> Liveries { get; set; }
[ForeignKey("Id")]
public AircraftLivery DefaultLivery { get; set; }
}
public class AircraftLivery : ModelBase
{
public byte[] Image { get; set; }
public string Name { get; set; }
[ForeignKey("Id")]
public AircraftType AircraftType { get; set; }
[ForeignKey("ID")]
public ICollection<AircraftLivery> Aircrafts { get; set; }
public string Author { get; set; }
}
public class SampleData : DropCreateDatabaseIfModelChanges<AirlineContext>
{
protected override void Seed(AirlineContext context)
{
var aircraft = new List<Aircraft>
{
//new Aircraft() {},
};
var aircraftTypes = new List<AircraftType>
{
//new AircraftType() {},
};
var aircraftLiveries = new List<AircraftLiveries>
{
//new AircraftLiveries() {},
};
}
}
In the Service:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Database.SetInitializer(new SampleData());
}
}
Upvotes: 0
Views: 605
Reputation: 3110
Just a simple hint, I hope it will help.
public class Aircraft : ModelBase
{
public Guid SerialNumber { get; set; }
public string Registration { get; set; }
public byte[] Image { get; set; }
//This is ForeignKey to AircraftType
public int AircraftTypeId {get;set;}
//This is ForeignKey to AircraftLivery
public int AircraftLiveryId {get;set;}
//Instead of Id use AircraftTypeId
[ForeignKey("AircraftTypeId")]
public AircraftType Type { get; set; }
[ForeignKey("AircraftLiveryId")]
public AircraftLivery Livery { get; set; }
}
for collection you have to use ICollection. e.g.
public class Category
{
[Key]
public int Id{get;set;}
public string Name {get;set;}
public ICollection<SubCategory> SubCategories{get;set;}
}
public class SubCategory
{
[Key]
public int Id{get;set;}
public string Name {get;set;}
public int CategoryId {get;set;}
[ForeignKey("CategoryId")]
public Category Category {get;set;}
}
Upvotes: 1