Entity Framework .Net Core 3.1 - 代码优先与脚手架
Entity Framework .Net Core 3.1 - Code First vs Scaffolding
我想要做的是在 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
}
问题是:
- 在 Code First 中实现关系的正确方法是什么?
- 由于我要实现的应用程序的数据库会非常大,在 SqlServer 中设计数据库然后继续构建表是不是一个好方法?
感谢支持
就我的观点来说,应该是依赖一个人,如何he/she才舒服。如果你擅长 SQL 方面,请先设计数据库,然后再设计脚手架。如果你擅长 C# 端,请使用代码优先方法
1) 没有评论,因为我从来没有用过它。
2) 数据库优先的方法是最有效的。节省大量时间。是的,在 SQL Server 中设计表,然后在 运行 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>();
}
}
这基本上就是首先在 EF 代码中设置关系的方法
总结
- 创建数据模型
- 创建数据库上下文
- 设置 EF
- 代码优先迁移
您可以在此处找到有关第 3 步和第 4 步的注释
Get Started with Entity Framework 6 Code First
对于问题的第二部分,您可以参考这些链接来权衡您的选择
what is advantage of CodeFirst over Database First
我想要做的是在 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
}
问题是:
- 在 Code First 中实现关系的正确方法是什么?
- 由于我要实现的应用程序的数据库会非常大,在 SqlServer 中设计数据库然后继续构建表是不是一个好方法?
感谢支持
就我的观点来说,应该是依赖一个人,如何he/she才舒服。如果你擅长 SQL 方面,请先设计数据库,然后再设计脚手架。如果你擅长 C# 端,请使用代码优先方法
1) 没有评论,因为我从来没有用过它。
2) 数据库优先的方法是最有效的。节省大量时间。是的,在 SQL Server 中设计表,然后在 运行 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>();
}
}
这基本上就是首先在 EF 代码中设置关系的方法
总结
- 创建数据模型
- 创建数据库上下文
- 设置 EF
- 代码优先迁移
您可以在此处找到有关第 3 步和第 4 步的注释 Get Started with Entity Framework 6 Code First
对于问题的第二部分,您可以参考这些链接来权衡您的选择
what is advantage of CodeFirst over Database First