简体   繁体   English

使用具有基本身份验证的 .NET 获取外部 API,并使用 EF Core 将数据存储在 SQL 服务器数据库中

[英]Fetch external API using .NET with basic authentication and store data in SQL Server database using EF Core

I'm not getting the data into my SQL Server database.我没有将数据放入我的 SQL 服务器数据库。 Hope someone can guide me.希望有人能指导我。 Thanks.谢谢。

In Visual Studio 2022:在 Visual Studio 2022 中:

Program : Program

using APItoSQL1.Model;
using System.Net.Http.Headers;

async Task<List<Party>> FetchParties()
{
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "username:password");

        var response = await client.GetAsync("url");
        response.EnsureSuccessStatusCode();

        var data = await response.Content.ReadAsAsync<List<Party>>();
        return data;
    }

    async Task SaveParties(List<Party> parties)
    {
        using (var context = new MyDbContext())
        {
            context.Parties.AddRange(parties);
            await context.SaveChangesAsync();
        }
    }

    var parties = await FetchParties();
    await SaveParties(parties);
}

My DbContext :我的DbContext

using Microsoft.EntityFrameworkCore;
using System.Data.Entity;
using System;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using System.Data.SqlClient;
using DbContext = Microsoft.EntityFrameworkCore.DbContext;

namespace APItoSQL1.Model
{
    public class MyDbContext : DbContext
    {
        public System.Data.Entity.DbSet<Party> Parties { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectionString = @"Server="myservername";Initial Catalog="myDBname";Integrated Security=True";
        }
    }
}

Class Party : Class Party

using System.Xml.Serialization;

namespace APItoSQL1.Model
{
    [XmlRoot(ElementName = "parties")]
    public class Party
    {
        [XmlElement(ElementName = "id")]
        public int Id { get; set; }

        [XmlElement(ElementName = "isOrganization")]
        public bool IsOrganization { get; set; }

        [XmlElement(ElementName = "primeName")]
        public string PrimeName { get; set; }

        [XmlElement(ElementName = "email")]
        public string Email { get; set; }

        [XmlElement(ElementName = "givenName")]
        public string GivenName { get; set; }

        [XmlElement(ElementName = "phone")]
        public string Phone { get; set; }
    }
}

Have I missed something?我错过了什么吗? I'd appreciate any help on this.我很感激这方面的任何帮助。

Thanks in advance.提前致谢。

You are mixing "classic" Entity Framework and EF Core:您正在混合使用“经典”实体框架和 EF Core:

Remove the reference to EntityFramework from your project, and remove this using:从您的项目中删除对 EntityFramework 的引用,并使用以下方法删除它:

using System.Data.Entity;

public DbSet<Party> Parties { get; set; }

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

相关问题 首先使用ef核心代码将密码存储在sql服务器数据库中 - storing passwords in sql server database using ef core code first 使用 EF Core 将数据从 Excel 移动到 SQL 服务器数据库的效率建议 - Efficiency Suggestions for Moving Data from Excel to SQL Server Database using EF Core 如何使用现有 SQL 查询从 MS SQL 获取 .NET Core WEP API 项目中的数据? - How to fetch data in .NET Core WEP API project from MS SQL, using existing SQL queries? 无法使用 EF Core 从 MS SQL 服务器检索数据 - Cannot retrieve data from MS SQL Server using EF Core 将信息保存在数据库中,然后使用 .NET CORE 将请求发布到外部 API - Saving the information in database and then Post request to external API using .NET CORE .Net Core 自定义身份验证使用 API 密钥和 Identity Server 4 - .Net Core Custom Authentication using API Keys with Identity Server 4 如何使用 Asp.net Core EF Core 在 Web 托管服务器上创建多个数据库 - How To Create multiple database on web hosting server using Asp.net core EF Core 使用数据库的基本身份验证 - Basic Authentication using database 如何使用 EF 内核在 Asp.net 内核 web API 中保存相关模型中的数据? - How to save Data in Related Models in Asp.net core web API using EF core? .Net Core - 如何使用 EF Core 添加 SQL 列约束 - .Net Core - How to add SQL Column Constraint using EF Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM