Ce que j'essaie de faire, c'est d'implémenter le modèle dans Code First pour ces entités de base:
public class CompanyType{ public int Id {get;set;} public string CompanyType {get;set;} } public class Company{ public int Id {get;set;} public string Company {get;set;} public string idCompanyType {get;set;} //Related 1-1 to CompanyType } public class Employee{ public int Id {get;set;} public string Company {get;set;} public int idCompany {get;set;} // --> Here I have to relate idCompany with CompanyId ( 1 company , N Employee }
Les questions sont:
Merci à l'assistance
3 Réponses :
En ce qui concerne mon point de vue, devrait dépendre d'une personne, comment il / elle est à l'aise. Si vous êtes doué du côté SQL, concevez d'abord db puis échafaudage. Si vous êtes bon côté c #, utilisez d'abord l'approche par le code
Pour implémenter des relations, utilisez couramment pour pouvoir ajouter des migrations. Jetez un œil à ceci: learnentityframeworkcore.com/migrations . Et l'API Fluent, reportez-vous à: docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/...
Merci Richards, si vous pratiquez, pouvez-vous me montrer par code comment faire?
1) Pas de commentaire car je ne l'utilise jamais.
2) L'approche Database-First est la plus efficace à faire. Économisez beaucoup de votre temps. Oui, créez des tables dans SQL Server, puis exécutez Scaffold-DbContext.
public class CompanyType{ public int Id {get;set;} public string CompanyType {get;set;} } public class Company{ public Company() { Employees = new HashSet<Employee>(); } public int Id {get;set;} public string Company {get;set;} public int CompanyTypeID public virtual CompanyType CompanyType {get;set;} public virtual ICollection<Employee> Employees { get; set; } } public class Employee { public int Id {get;set;} public int CompanyID {get;set;} public virtual Company Company {get;set;} } public class SomeContext : DbContext { public SomeContext() : base("SomeContext") { } public DbSet<CompanyType> CompanyTypeSet { get; set; } public DbSet<Employee> EmployeeSet { get; set; } public DbSet<Company> CompanySet { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } This is basically how to set up your relations in EF code firstSummary Create Data Model Create Database Context Setup EF Code first Migration you can find notes on step 3 and 4 here Get Started with Entity Framework 6 Code First for the second part of your question you can refer to these links to weigh your optionswhat is advantage of CodeFirst over Database FirstEF Code-First Approach Vs Database-First Approach3 reasons to use code first design
Merci Francisco, vraiment apprécié!