简体   繁体   English

SQL Server表设计建议(Entity Framework代码优先,C#)

[英]SQL Server table design advice (Entity Framework code-first, C#)

I have started working on a website created by another development house. 我已经开始在另一个开发公司创建的网站上工作。 The database structure was created using Entity Framework's code first approach. 数据库结构是使用实体框架的代码优先方法创建的。 In the C# code we have something along the lines of: 在C#代码中,我们遵循以下原则:

public class MainBusinessObject
{
    ...
    public SubBusinessObject { get; set; }
}

public abstract class SubBusinessObject 
{
    ...
}

public class SubBusinessObjectTypeOne : SubBusinessObject 
{
    ....
}

public class SubBusinessObjectTypeTwo : SubBusinessObject 
{
    ...
}

...

There are 6 SubBusinessObject types. 有6种SubBusinessObject类型。 The smallest has one property, the largest around 50. The SubBusinessObject abstract class defines an additional 17 properties. 最小的具有一个属性,最大的具有约50个属性SubBusinessObject抽象类定义了另外的17个属性。 The MainBusinessObject has around 35. MainBusinessObject大约有35个。

What sort of real world entities do these represent? 这些代表什么样的现实世界实体? If MainBusinessObject held the data for, say, FruitForSale , then SubBusinessObject would represent Fruit , and SubBusinessObjectTypeOne could be Apple , SubBusinessObjectTypeTwo could be Orange and so on. 如果MainBusinessObject举行,可以说,数据FruitForSale ,然后SubBusinessObject将代表FruitSubBusinessObjectTypeOne可能是AppleSubBusinessObjectTypeTwo可能是Orange等。

Both the MainBusinessObject and SubBusinessObject classes link to other database objects (such as the source and location of the items). MainBusinessObjectSubBusinessObject类都链接到其他数据库对象(例如项目的源和位置)。 All of these entities map directly to SQL Server tables. 所有这些实体都直接映射到SQL Server表。

As a function of this design queries dealing with a MainBusinessObject are slow. 根据此设计,处理MainBusinessObject查询很慢。 For instance it takes around half a second to retrieve the data for a single MainBusinessObject (and nearly 5 seconds on first run). 例如,检索单个MainBusinessObject的数据大约需要MainBusinessObject (第一次运行将近5秒)。 I have tried making multiple calls however the performance ends up being roughly the same. 我曾尝试拨打多个电话,但性能最终大致相同。 The site in general suffers from poor performance and this is a key area. 该站点通常表现不佳,这是一个关键区域。

Please note that I am not a DBA, I am just a developer (and we all know how well developers design databases...). 请注意,我不是DBA,我只是一名开发人员(而且我们都知道开发人员在设计数据库方面做得如何……)。 However the table design appears overly convoluted to me, though I could well be wrong. 然而,尽管我很可能错了,但桌子的设计似乎对我来说却过于复杂。 Would flattening these tables into a single table be considered bad practice? 将这些表展平为一个表是否被视为不良做法? Any advice around dealing with these sorts of issues would be very much appreciated. 任何有关处理此类问题的建议将不胜感激。

Would flattening these tables into a single table be considered bad practice? 将这些表展平为一个表是否被视为不良做法?

No, that would not be considered a bad practice. 不,这不会被视为不良做法。

EF 6 supports two strategies for storing an inheritance hierarchy like this: Table-per-Type (TPT) and Table-per-Hierarchy (TPH). EF 6支持两种存储继承层次结构的策略,例如: 每类型 (TPT)和每层次表 (TPH)。 TPT storage is initially attractive as each subtype gets its own table in the database. TPT存储最初很有吸引力,因为每个子类型在数据库中都有自己的表。 In TPH a single table stores all the attributes, with a discriminator column. 在TPH中,一个表存储所有属性,并带有一个鉴别符列。

But, as I think you are discovering, joining these tables at runtime can be expensive. 但是,正如我认为您发现的那样,在运行时加入这些表可能会很昂贵。

TPH is generally considered a best-practice, as less prone to performance problems. TPH通常被认为是最佳实践,因为它不太容易出现性能问题。 And so, no, flattening all these tables is not considered a bad practice. 因此,不行,将所有这些表展平认为是不好的做法。 In fact TPT has not yet been implemented in EF Core, for this reason. 实际上,由于这个原因,TPT尚未在EF Core中实现。

You should create a new version of the database using TPH and copy data from the current database to the new one for testing. 您应该使用TPH创建数据库的新版本,并将数据从当前数据库复制到新数据库以进行测试。 If you want to transition your read database to TPH, here's a helpful post on how to do that with a Migration. 如果您想将读取的数据库转换为TPH,这是有关如何进行迁移的有用文章。 Entity Framework Code First Convert TPT to TPH 实体框架代码首先将TPT转换为TPH

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先迁移与SQL Server失败 - Entity Framework Code-First Migration Fails with SQL Server C#交互式窗口中的实体框架Code-First(Oracle) - Entity framework Code-First in c# interactive window (Oracle) C#-实体框架-大种子数据代码优先 - C# - Entity Framework - Large seed data code-first 将列添加到 C# 和实体框架代码中的表中,而不删除数据库内容 - Add column to table in C# & Entity Framework code-first without deleting DB content 域驱动设计和实体框架4.1(代码优先) - Domain Driven Design and Entity Framework 4.1 (code-first) 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship 使用代码优先迁移的Entity Framework从SQL Server到Oracle - From SQL Server to Oracle using Entity Framework with code-first Migrations 实体框架代码优先定位LocalDb而不是本地SQL Server Express主机 - Entity Framework code-first targeting LocalDb instead of local SQL Server Express host 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? C#实体框架与该类中相同类的代码优先关系 - C# Entity Framework code-first relationship to same class within the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM