简体   繁体   English

如何将列表从ASP.NET Core存储到SQL服务器?

[英]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.话虽如此,还是有办法解决的。

  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.将列表保存为服务器上的文件,然后将文件的位置存储在数据库中。

暂无
暂无

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

相关问题 ASP.Net Core - 在 SQL Server 数据库中存储和检索 FormCollection - ASP.Net Core - Store and Retrieve FormCollection in SQL Server database "如何编写参数为字符串列表的存储过程 [ SQL Server,ASP.NET Core 3.1 ]" - How to write a stored procedure in which the parameters are a list of strings [ SQL Server, ASP.NET Core 3.1 ] 如何在 ASP.NET Core 中的 SQL Server 数据库中创建照片列表? - How to create list of photos in SQL Server database in ASP.NET Core? 如何从在容器中运行的 ASP.NET 核心应用程序连接到具有集成安全性的 Windows 服务器上的 SQL 服务器 - How to connect from ASP.NET Core application running in a container to SQL Server on Windows Server with Integrated Security ASP.NET 核心如何上传图片到SQL服务器数据库? - ASP.NET Core how to upload image to SQL Server database? 将 ASP.NET 内核连接到 SQL 服务器 - Connecting ASP.NET Core to SQL server 如何在 ASP.NET 内核中存储路由? - How to store a route in ASP.NET Core? 如何使用来自同一ASP.NET Core C#代码的不同数据库(SQL Server,Oracle) - How to use different databases (SQL Server, Oracle) from the same ASP.NET Core C# code 如何通过C#ASP.net从SQL Server中的文本框存储日期 - How to store date from text box in SQL Server through C# ASP.net ASP.NET MVC 使用 WCF 从 SQL Server 获取数据:如何在控制器中放置临时数据存储 - ASP.NET MVC get data from SQL Server with WCF : how to put temporary data store in controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM