1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace GroupeVaritronLib.Packaging
{
public interface IContainable {}
public interface IContainer {}
public interface IPallet : IContainer
{
string pallet_no { get; set; }
DateTime pallet_date { get; set; }
decimal id { get; set; }
}
public class Pallet : IPallet
{
public string pallet_no { get; set; }
public DateTime pallet_date { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public decimal id { get; set; }
}
public class Box : IContainer, IContainable
{
public string box_no { get; set; }
public DateTime box_date { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public decimal id { get; set; }
public decimal? main_box_id { get; set; }
public virtual Box main_box { get; set; }
public decimal? pallet_id { get; set; }
public virtual Pallet pallet { get; set; }
}
public interface IBox : IContainable, IContainer
{
string box_no { get; set; }
DateTime box_date { get; set; }
decimal id { get; set; }
decimal? main_box_id { get; set; }
IBox main_box { get; set; }
decimal? pallet_id { get; set; }
IPallet pallet { get; set; }
}
public interface IItem : IContainable
{
decimal id { get; set; }
string item_no { get; set; }
DateTime item_date { get; set; }
decimal? box_id { get; set; }
IBox box { get; set; }
decimal? pallet_id { get; set; }
IPallet pallet { get; set; }
string GetItemNo();
}
public class Item : IItem
{
public string item_no { get; set; }
public DateTime item_date { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public decimal id { get; set; }
public decimal? box_id { get; set; }
public virtual IBox box { get; set; }
public decimal? pallet_id { get; set; }
public virtual IPallet pallet { get; set; }
public virtual string GetItemNo()
{
return "ITEM0001";
}
}
public abstract class PackagingContext : DbContext, IDisposable
{
public PackagingContext(string connstring, string ProductName)
: base(connstring)
{
_ProductName = ProductName;
}
public PackagingContext()
{ }
private string _ProductName;
public string ProductName
{
get { return _ProductName.ToUpper(); }
set { value = _ProductName; }
}
///<summary*>
///Méthode permettant de contrôler la création des tables dans la base de données
///</summary*>
///<remarks>
///Cette méthode provient de la classe de base DbContext et peut être personnalisée
///</remarks>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Pallet>().Property(P => P.id).HasPrecision(18, 0);
//Convention de nom de table SQL server (CF = Code First) : dbo.NOMDEPRODUIT_PALLETS_CF
//Notez que le nom de table est tout en majuscule, convention utilisée pour uniformiser la procédure
modelBuilder.Entity<Pallet>().ToTable(ProductName + "_PALLETS" + "_CF");
modelBuilder.Entity<Box>().Property(P => P.id).HasPrecision(18, 0);
modelBuilder.Entity<Box>().ToTable(ProductName + "_BOXES" + "_CF");
modelBuilder.Entity<Box>().HasOptional(x => x.pallet).WithMany().HasForeignKey(r => r.pallet_id);
modelBuilder.Entity<Box>().HasOptional(x => x.main_box).WithMany().HasForeignKey(c => c.main_box_id);
modelBuilder.Entity<Item>().Property(P => P.id).HasPrecision(18, 0);
modelBuilder.Entity<Item>().ToTable(ProductName + "_ITEMS" + "_CF");
modelBuilder.Entity<Item>().HasOptional(x => x.box).WithMany().HasForeignKey(r => r.box_id);
modelBuilder.Entity<Item>().HasOptional(x => x.pallet).WithMany().HasForeignKey(r => r.pallet_id);
}
public DbSet<Pallet> Pallets { get; set; }
public DbSet<Box> Boxes { get; set; }
public DbSet<Item> Items { get; set; }
}
} |
Partager