Last active
July 12, 2018 10:46
-
-
Save KarateJB/c2a0060622a8aafb75dc35566eade3da to your computer and use it in GitHub Desktop.
[Entity Framework Code first] Relations (Data Annotation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Table("Suppliers")] | |
public class Supplier | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | |
public int Id { get; set; } | |
[StringLength(100)] | |
public string CompanyName { get; set; } | |
[StringLength(50)] | |
public string Tel { get; set; } | |
[StringLength(200)] | |
public string Address { get; set; } | |
#region One-to-one | |
public virtual SupplierInCharge InCharge { get; set; } | |
#endregion | |
#region One-to-many | |
public virtual ICollection<Factory> Factorys { get; set; } | |
#endregion | |
#region Many-to-many | |
public virtual ICollection<Customer> Customers { get; set; } | |
#endregion | |
} | |
[Table("SupplierInCharges")] | |
public class SupplierInCharge | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.None)] | |
[ForeignKey("Supplier")] | |
public int Id { get; set; } | |
[StringLength(20)] | |
public string Name { get; set; } | |
[StringLength(50)] | |
public string Phone { get; set; } | |
#region One-to-one | |
public virtual Supplier Supplier { get; set; } | |
#endregion | |
} | |
[Table("Factorys")] | |
public class Factory | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | |
public int Id { get; set; } | |
[StringLength(50)] | |
public string Name { get; set; } | |
[StringLength(200)] | |
public string Address { get; set; } | |
[Required] | |
public int SupplierId { get; set; } | |
#region Many-to-one | |
[ForeignKey("SupplierId")] | |
public virtual Supplier Supplier { get; set; } | |
#endregion | |
} | |
[Table("Customers")] | |
public class Customer | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | |
public int Id { get; set; } | |
[StringLength(100)] | |
public string Name { get; set; } | |
[StringLength(200)] | |
public string Address { get; set; } | |
#region Many-to-many | |
public virtual ICollection<Supplier> Suppliers { get; set; } | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment