0

私は n-Teir プロジェクトに取り組んでおり、この質問はサービス (ビジネス ロジック) とデータ レイヤーに焦点を当てています。データ層には、Aircraft、AircraftType、AircraftLivery の 3 つのモデルがあります。これで、サンプル データを使用してデータベースを初期化する方法についてかなり良いアイデアが得られましたが、さまざまな型を関連付ける方法がわかりません。どのようにカラーリングを作成し、それを航空機または航空機タイプのデフォルトのカラーリングに関連付けますか? 航空機のタイプを航空機に関連付けるにはどうすればよいですか? 以下は現在ビルドされているコードですが、まだ実行していません。最後のクラス、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());
        }
    }
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 に答える