实体框架代码优先数据库初始化与关系
本文关键字:初始化 关系 数据库 框架 代码 实体 | 更新日期: 2023-09-27 18:21:33
我正在做一个n-Teir项目,这个问题的重点是服务(Buisness逻辑)和数据层。我在数据层中有三个模型:Aircraft、AircraftType和AircraftLivery。现在我对如何用示例数据初始化数据库有了一个很好的想法,但我不确定如何将不同的类型关联起来。我将如何创建交付并将其与飞机或飞机类型的默认交付联系起来?如何将飞机类型与飞机联系起来?下面是当前生成的代码,但我还没有运行。我将如何实现最后一个类Sample Data?在这段代码中,您是否看到了其他可以采取不同做法的内容?
在数据层:
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() {},
};
}
}
在役:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Database.SetInitializer(new SampleData());
}
}
只是一个简单的提示,我希望它会有所帮助。
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; }
}
对于集合,您必须使用ICollection。例如
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;}
}