[英]How to store list in SQL Server from ASP.NET Core?
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:这是我的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?
}
}
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.因为如果它是一个文件存储或 NoSql 数据库那么它就相当容易了。
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.话虽如此,还是有办法解决的。
Store the list as a string:将列表存储为字符串:
var listString = JsonConvert.Serialize(MyList);
Then you can store that string in the database easily然后你可以很容易地将该字符串存储在数据库中
Save the list as a file on the server, then just store the location of the file in the database.将列表保存为服务器上的文件,然后将文件的位置存储在数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.