0

I have this table in my database and I want to store incoming data:

public string Id { get; set; } = default!;
public double? Name { get; set; } = default!;
public string? FamilyName { get; set; } = default!;
public List<Addresses> ClientAddress { get; set; } = default!;

Here is my Addresses class:

public string Id { get; set; } = default!;
public string Address { get; set; } = default!;

Now I want to store in database - how should I do with the list?

public Task<bool> StoreModel(MainModel mainmodel) 
{
     var listtostore = new MainModelEnity()
                           {
                               Name = mainmodel.Name,
                               FamilyName = mainmodel.FamilyName,
                               ClientAddress = //how should I store here? 
                           }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
moris62
  • 983
  • 1
  • 14
  • 41
  • It'd be great if you mentioned what type of database you're trying to use – OneCricketeer Sep 30 '21 at 19:45
  • @OneCricketeer SQLSERVER – moris62 Sep 30 '21 at 19:46
  • If you want to store lists, then why are you using a relational database? The "correct" pattern is to store individual rows of `store(address_id=1), store(address_id=2)` in some `store` table, then `address` table has rows for `id=1, id=2`, then you join these to get stores with address info – OneCricketeer Sep 30 '21 at 20:23
  • @OneCricketeer sorry but i did not get you,from UI get the model with multiple address for a single person,im using relational DB,you are saying i should not use List,but what should i do then?how can i get thoses address store in the db?can you make an example considering my updated question?i really appreciate your help – moris62 Sep 30 '21 at 21:11
  • I'm saying you should use a foreign key lookup and a SQL-join to get the complete dataset. Doesn't matter that you have a UI component to your app. A store table should only hold store data. Each store has an address, and a list of customers that have ordered something (for example). Each customer has a billing/shipping address. Those should really all be different tables (store, customer, order, address, and maybe others) – OneCricketeer Sep 30 '21 at 21:39
  • If there are multiple stores, each with different addresses, then each store has some unique identifier, therefore is a unique row, and each row would reference a different address (single string or ID), and **not** a list within the database row – OneCricketeer Sep 30 '21 at 21:43
  • @OneCricketeer there are clients with different addresses ,I have created another class and table for addresses,my question is when i get data from UI,it contains name,familyName and addresses(lets say 10 addresses)how do you store all this data using linq? – moris62 Sep 30 '21 at 21:50
  • I don't know LINQ. But the [comments below](https://stackoverflow.com/questions/69398019/how-to-store-list-in-sql-server-from-asp-net-core?noredirect=1#comment122662653_69398107) link you to how you define relationships between tables/entity classes – OneCricketeer Sep 30 '21 at 21:53
  • https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration – granadaCoder Oct 01 '21 at 11:08

1 Answers1

0

I assume you meant to ask how you store a list in a relational database? Because if if it is a file store or NoSql database then it's fairly easy.

However the short answer is, you don't. It's not what a relational database was meant to do. See this Post.

That being said, there are ways around it.

  1. Store the list as a string:

     var listString = JsonConvert.Serialize(MyList);
    

    Then you can store that string in the database easily

  2. Save the list as a file on the server, then just store the location of the file in the database.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CorrieJanse
  • 2,374
  • 1
  • 6
  • 23
  • thanks but i created a tables for them ,as you can see addresses class is a foreign key in the main class,so,still i need to do jsonconvert?to be honest i have no idea if my approach is correct – moris62 Sep 30 '21 at 19:53
  • 1
    if i dont wana create a second table i need to do public List ClientAddress ,and when i want to create a DB it throws an error that i cant create table based on list entity – moris62 Sep 30 '21 at 19:55
  • I don't think OP wants to store JSON or a literal list. The [documentation of EntityFramework shows using Lists to define relationships](https://learn.microsoft.com/en-us/ef/core/modeling/relationships) – OneCricketeer Sep 30 '21 at 21:49