Skip to content

Instantly share code, notes, and snippets.

@KarateJB
Last active July 12, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KarateJB/c2a0060622a8aafb75dc35566eade3da to your computer and use it in GitHub Desktop.
Save KarateJB/c2a0060622a8aafb75dc35566eade3da to your computer and use it in GitHub Desktop.
[Entity Framework Code first] Relations (Data Annotation)
[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