C# 实体框架.Net核心3.1-代码优先与脚手架
我要做的是首先用代码实现这个基本实体的模型:C# 实体框架.Net核心3.1-代码优先与脚手架,c#,.net-core,entity-framework-6,entity-framework-core,C#,.net Core,Entity Framework 6,Entity Framework Core,我要做的是首先用代码实现这个基本实体的模型: 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;s
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
}
问题是:
感谢支持在我看来,应该取决于一个人,他/她是否舒适。若你们擅长SQL端,那个么先设计数据库,然后设计脚手架。如果你擅长c#side,请使用代码优先的方法在我看来,应该取决于一个人,他/她是否感到舒适。若你们擅长SQL端,那个么先设计数据库,然后设计脚手架。如果你擅长c#side,请使用代码优先的方法,不要评论,因为我从不使用它 2) 数据库优先的方法是最有效的方法。节省很多时间。是的,在SQL Server中设计表,然后运行Scaffold DbContext。1)没有注释,因为我从未使用过它
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>();
}
}
2) 数据库优先的方法是最有效的方法。节省很多时间。是的,在SQL Server中设计表,然后运行Scaffold DbContext。公共类CompanyType{
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>();
}
}
公共int Id{get;set;}
公共字符串CompanyType{get;set;}
}
公营公司{
上市公司()
{
Employees=newhashset();
}
公共int Id{get;set;}
公共字符串公司{get;set;}
public int CompanyTypeID
公共虚拟公司类型公司类型{get;set;}
公共虚拟ICollection雇员{get;set;}
}
公营雇员{
公共int Id{get;set;}
public int CompanyID{get;set;}
公共虚拟公司公司{get;set;}
}
公共类SomeContext:DbContext{
public SomeContext():基(“SomeContext”)
{
}
public DbSet CompanyTypeSet{get;set;}
公共DbSet EmployeeSet{get;set;}
公共DbSet CompanySet{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
这基本上就是如何首先在EF代码中建立关系
摘要
公共类公司类型{
公共int Id{get;set;}
公共字符串CompanyType{get;set;}
}
公营公司{
上市公司()
{
Employees=newhashset();
}
公共int Id{get;set;}
公共字符串公司{get;set;}
public int CompanyTypeID
公共虚拟公司类型公司类型{get;set;}
公共虚拟ICollection雇员{get;set;}
}
公营雇员{
公共int Id{get;set;}
public int CompanyID{get;set;}
公共虚拟公司公司{get;set;}
}
公共类SomeContext:DbContext{
public SomeContext():基(“SomeContext”)
{
}
public DbSet CompanyTypeSet{get;set;}
公共DbSet EmployeeSet{get;set;}
公共DbSet CompanySet{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
这基本上就是如何首先在EF代码中建立关系
摘要
要实现关系,请使用fluent以便添加迁移。看看这个:。和Fluent API,参考:谢谢Richards,如果你在练习,你能用代码告诉我怎么做吗?要实现关系,请使用Fluent以便添加迁移。看看这个:。和流畅的API,请参考:谢谢Richards,如果你在练习,你能用代码告诉我怎么做吗?谢谢Francisco,非常感谢!谢谢弗朗西斯科,真的很感激!