Skip to content

Instantly share code, notes, and snippets.

@KarateJB
Last active July 12, 2018 10:47
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/c0ea7c89f98288fdf2871ccf99c73df5 to your computer and use it in GitHub Desktop.
Save KarateJB/c0ea7c89f98288fdf2871ccf99c73df5 to your computer and use it in GitHub Desktop.
[Entity Framework Code first] Relations (Fluent API)
[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)]
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
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
}
public class AppDbContext : System.Data.Entity.DbContext
{
public AppDbContext()
: base("name=AppDbContext")
{
}
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<Factory> Factorys { get; set; }
public DbSet<SupplierInCharge> SupplierInCharges { get; set; }
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
#region Relations
//One-to-one
modelBuilder.Entity<Supplier>().HasRequired(m => m.InCharge).WithRequiredPrincipal(m => m.Supplier);
//One-to-many
modelBuilder.Entity<Supplier>().HasMany(m => m.Factorys).WithRequired(m => m.Supplier);
//Many-to-many
modelBuilder.Entity<Supplier>()
.HasMany<Customer>(s => s.Customers)
.WithMany(c => c.Suppliers)
.Map(cs =>
{
cs.MapLeftKey("SupplierId");
cs.MapRightKey("CustomerId");
cs.ToTable("SupplierCustomers");
});
#endregion
#region Cascade setting
modelBuilder.Entity<Supplier>().HasRequired(i => i.InCharge)
.WithRequiredPrincipal(i => i.Supplier).WillCascadeOnDelete(true);
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment