0

我正在做一个 n-Teir 项目,这个问题的重点是服务(业务逻辑)和数据层。我在数据层中有三个模型:Aircraft、AircraftType 和 AircraftLivery。现在我对如何使用示例数据初始化数据库有了一个很好的想法,但我不确定如何关联不同的类型。我将如何创建涂装并将其与飞机或飞机类型的默认涂装相关联?如何将飞机类型与飞机联系起来?下面是当前构建的代码,但我还没有运行。我将如何实现最后一个类,示例数据?这段代码中是否还有其他可以以不同方式完成的内容?

在数据层中:

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

1 回答 1

1

只是一个简单的提示,我希望它会有所帮助。

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;}
}
于 2013-07-07T08:20:18.263 回答