Last active
July 12, 2018 10:47
-
-
Save KarateJB/c0ea7c89f98288fdf2871ccf99c73df5 to your computer and use it in GitHub Desktop.
[Entity Framework Code first] Relations (Fluent API)
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)] | |
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 | |
} |
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
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