简体   繁体   English

如何在数据库中存储带有数组的ac#对象?

[英]How to store a c# object with arrays in a database?

I'm currently developing a software with C# and want it to store the data in a database. 我目前正在使用C#开发一个软件,并希望它将数据存储在数据库中。 My problem is that i'm looking for the best approach to store the data of an object that contains two arrays. 我的问题是我正在寻找存储包含两个数组的对象数据的最佳方法。 The objects in the array look exactly the same but they got a different meaning. 数组中的对象看起来完全相同,但它们有不同的含义。

As an information: The data of the objects in the data is changing regularly. 作为信息:数据中对象的数据正在定期更改。

For example i got the following two classe: 例如,我得到以下两个classe:

public class ObjectA
{
    public string Text { get; set; }
    public int Value { get; set; }
}

public class ObjectB
{
    public string Text { get; set; }
    public int Value { get; set; }
}

public class ObjectC
{
    public string Name { get; set; }
    public List<ObjectA> DetailsA { get; set; }
    public List<ObjectB> DetailsB { get; set; }
}

Please note that i currently got over 24000 objects from ObjectC. 请注意,我目前从ObjectC获得了超过24000个对象。 The amount of objects that each array of those objects contains can vary quite a lot (up to 200 and maybe more in the future). 这些对象的每个数组包含的对象数量可能会有很大差异(最多200个,将来可能更多)。 Could this lead to any problems with the maximum row count if i use one of the solutions? 如果我使用其中一个解决方案,这会导致最大行数的任何问题吗?

I got the following two ideas of how the database shema could look like: 我有以下两个关于数据库shema如何看起来的想法:

  1. All attributes in one table. 一个表中的所有属性。

    Create a coloumn for each attribute of ObjectA and ObjectB so that i can store each object in the array in one row. 创建的每个属性的coloumn ObjectAObjectB ,这样我可以在阵列中的每个对象存储在一行。

     CREATE TABLE `data` ( `data_id` INT NOT NULL, `name_a` NVARCHAR(50) NOT NULL, `text_a` NVARCHAR(100) NOT NULL, `name_b` NVARCHAR(50) NOT NULL, `text_b` NVARCHAR(100) NOT NULL, `value` INT NOT NULL, PRIMARY KEY (`data_id`) ) 

    In this case i would store the value of name redundantly. 在这种情况下,我会冗余地存储name的值。

  2. Creating a foreign key in the table for ObjectA 在ObjectA的表中创建外键

    By doing this i could avoid storing the data from ObjectC redundantly while having to use joins when querying the data. 通过这样做,我可以避免冗余地存储来自ObjectC的数据,同时在查询数据时必须使用连接。

     CREATE TABLE `data` ( `data_id` INT NOT NULL AUTO_INCREMENT, `name` NVARCHAR(50) NOT NULL, PRIMARY KEY (`data_id`) ) CREATE TABLE `details_a` ( `a_id` INT NOT NULL AUTO_INCREMENT, `text` NVARCHAR(100) NOT NULL, `value` INT NOT NULL, `data_fk` INT NOT NULL, PRIMARY KEY (`a_id`) ) CREATE TABLE `details_b` ( `b_id` INT NOT NULL AUTO_INCREMENT, `text` NVARCHAR(100) NOT NULL, `value` INT NOT NULL, `data_fk` INT NOT NULL, PRIMARY KEY (`b_id`) ) 

Also, would it be better to create a new row in the database for each time the data of the arrays has changed, or should I change the existing data? 另外,每次数组数据发生变化时,最好在数据库中创建一个新行,还是应该更改现有数据?

Edit: Added some information about the amount of objects (right below the c# example). 编辑:添加了一些有关对象数量的信息(在c#示例的正下方)。

(moved from comment to answer, got too long) (从评论转到答案,太长了)

Approach 1 does not work with lists of As or Bs btw - you need at least your 2nd version with fks. 方法1不适用于As或Bs btw的列表 - 您至少需要使用fks的第二个版本。

Using joins is good, database normalization is good as well. 使用连接很好,数据库规范化也很好。 It partitions your data and allows the DB to optimize queries more better. 它可以对数据进行分区,并允许数据库更好地优化查询。

As a third way you could use a descriminator-column and store A and B in the same dbtable - the discriminator tells you if its a A or a B. 作为第三种方法,您可以使用descriminator-column并将A和B存储在同一个dbtable中 - 鉴别器会告诉您它是A还是B.

CREATE TABLE `details_ab` (
  `a_id`        INT            NOT NULL    AUTO_INCREMENT,
  `text`        NVARCHAR(100)  NOT NULL,
  `value`       INT            NOT NULL,
  'isA'         BIT            NOT NULL,   -- True == A else B 
  `data_fk`     INT            NOT NULL,
  PRIMARY KEY (`a_id`)
)

The normal/lazy approach is to use / configure an orm mapper to do this for you so you can treat your DB as C# objects and let the details be handled by the orm mapper (google EntityFramework, Hibernate, code-first, data-first, fe here: msdn.microsoft.com/en-us/library/jj200620(v=vs.113).aspx ). 正常/惰性方法是使用/配置orm映射器为您执行此操作,以便您可以将数据库视为C#对象,并让orm映射器处理详细信息(google EntityFramework,Hibernate,代码优先,数据优先) ,fe在这里:msdn.microsoft.com/en-us/library/jj200620(v=vs.113).aspx)。


As for the insert or update - it depends. 至于插入或更新 - 取决于。 If you have to (legally fe or for insurance) have to have the complete train of data in your DB you could use a table and ever only insert - and a view on top, that diplays the "newest" version of a dataitem. 如果你必须(合法的fe或保险)必须在你的数据库中拥有完整的数据系列,你可以使用一个表,并且只能插入 - 并且顶部的视图,它覆盖了数据项目的“最新”版本。

ie every row has a ID and you have a "BusinessID" that marks "an object" uniquely for you - you insert it the first time, then you modify it: this inserts it with a new DB-Id and the same "BusinessID". 即每行都有一个ID,你有一个“BusinessID”,它为你唯一标记“一个对象” - 你第一次插入它,然后你修改它:这将插入一个新的DB-Id和相同的“BusinessID” 。 You create a view on the table that only shows the highest DB-ID of each BusinessID (assuming integer DB-Ids). 您在表上创建一个仅显示每个BusinessID的最高DB-ID的视图(假设整数DB-Ids)。 You have the whole history in your table, but your application "normally" only sees the view, but maybe admins might get access to the whole table instead of the view ... etc. 您在表中拥有完整的历史记录,但您的应用程序“通常”只能看到视图,但管理员可能可以访问整个表而不是视图...等。

Creating the table structure with foreign keys is probably the best solution, and it would be the most commonly implemented. 使用外键创建表结构可能是最好的解决方案,它将是最常用的。 That said, I know needs can vary based on usage case, so here's one possible alternative. 也就是说,我知道需求会根据使用情况而有所不同,所以这里有一个可能的选择。

Depending on the usage and the size, I've found it helpful to store objects and variables as XML serialized equivalents in the DB. 根据用途和大小,我发现在DB中将对象和变量存储为XML序列化等价物会很有帮助。 If you just need it for persistent storage, that's the simplest method and then you could save versioning with a simple table holding a timestamp and the XML object. 如果您只需要它用于持久存储,那么这是最简单的方法,然后您可以使用包含时间戳和XML对象的简单表来保存版本控制。 In a pinch, you can even query your XML objects with SQL to access the data or migrate the storage method if needs change later. 在紧要关头,您甚至可以使用SQL查询XML对象以访问数据,或者在以后需要更改时迁移存储方法。 I use this method for storing dictionary style data with different data types in a single table for quick access to environmental variables. 我使用此方法在单个表中存储具有不同数据类型的字典样式数据,以便快速访问环境变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM