[英]Entity Framework .Net Core 3.1 - Code First vs Scaffolding
What I'm trying to do is implementing the model in Code First for this basic entities:我想要做的是在 Code First 中为这个基本实体实现模型:
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
}
Questions are:问题是:
Thanks to support感谢支持
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 first这基本上是如何首先在 EF 代码中设置您的关系
Summary概括
you can find notes on step 3 and 4 here Get Started with Entity Framework 6 Code First您可以在此处找到有关第 3 步和第 4 步的注释, 首先使用实体框架 6 代码
for the second part of your question you can refer to these links to weigh your options对于问题的第二部分,您可以参考这些链接来权衡您的选择
what is advantage of CodeFirst over Database First CodeFirst 相对于 Database First 的优势是什么
EF Code-First Approach Vs Database-First Approach EF 代码优先方法与数据库优先方法
As far as my point of view , should be dependant on a person, how he/she is comfortable.在我看来,应该取决于一个人,他/她有多舒服。 If you are good at SQL side, design db first then scaffold.如果你擅长 SQL 端,先设计 db,再设计脚手架。 If you are good at c# side, use code first approach如果您擅长 c# 方面,请使用代码优先方法
1) No comment as I never use it. 1)没有评论,因为我从不使用它。
2) Database-First approach is the most effective to do. 2)数据库优先方法是最有效的方法。 Save alot of your time.节省您的大量时间。 Yes, design tables in SQL Server, then run Scaffold-DbContext.是的,在 SQL Server 中设计表,然后运行 Scaffold-DbContext。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.