简体   繁体   English

实体框架 .Net Core 3.1 - 代码优先与脚手架

[英]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:问题是:

  1. What is the correct way to implement the relations in Code First ?在 Code First 中实现关系的正确方法是什么?
  2. Since the database of the application I have to realize will be very big, Can be a good approach designing the database in SqlServer and then proceed to scaffold the tables ?由于我必须实现的应用程序的数据库会非常大,在 SqlServer 中设计数据库然后继续对表进行脚手架是一个好方法吗?

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概括

  1. Create Data Model创建数据模型
  2. Create Database Context创建数据库上下文
  3. Setup EF设置EF
  4. Code first Migration代码优先迁移

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 代码优先方法与数据库优先方法

3 reasons to use code first design 使用代码优先设计的 3 个理由

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.

相关问题 Entity Framework Core 3.1 脚手架生成隐藏列 - Entity Framework Core 3.1 Scaffolding Generating HIDDEN Column ASP.NET 核心实体框架代码到数据库脚手架无法识别 Id 属性或 [Key] 注释 - ASP.NET Core Entity Framework code-to-database scaffolding not recognizing Id property or [Key] annotation 数据库优先实体框架核心的自定义脚手架逻辑 - Custom scaffolding logic for database first Entity Framework Core 带有 Entity Framework Core 的 SQLite 脚手架 - SQLite scaffolding with Entity Framework Core 实体框架核心 - 自定义脚手架 - Entity Framework Core - Customise Scaffolding 实体框架核心 - 脚手架枚举 - Entity framework Core - Scaffolding enum Entity Framework Core 自定义脚手架 - Entity Framework Core Customize Scaffolding Net Core:Entity Framework 和 SQL 服务器临时表,自动脚手架 - Net Core: Entity Framework and SQL Server Temporal Tables, Automatic Scaffolding .NET Core 3.1 的自定义脚手架模板位置 - Custom scaffolding template location for .NET Core 3.1 如何在 VS Code 中将 .net 核心项目的目标框架从 netstandard2.1 更改为 netcoreapp3.1 - How to change target framework of a .net core project from netstandard2.1 to netcoreapp3.1 in VS Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM