RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[asp.net]

Martin Hope
Маг-притворщик
Asked: 2023-06-24 15:09:02 +0000 UTC

IServiceCollection是一个服务定位器?

  • 5

在这里,我将 IRepository<Network> 服务添加到服务容器,然后该类AdminController使用它。这个实现是反模式吗локатор сервисов?

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddTransient<IRepository<Network>, EFRepository<Network>>();

    ...
}
public class AdminController : Controller
{
    private readonly IRepository<Network> _dbContext;

    public AdminController(IRepository<Network> dbContext)
    {
        _dbContext = dbContext;
    }
} 

asp.net
  • 1 个回答
  • 20 Views
Martin Hope
MrPikokon
Asked: 2022-09-18 05:39:08 +0000 UTC

ASP.NET + Entity Framework - 使用数据库访问

  • 0

我将数据库上下文添加到服务中。我可以从 app.MapGet() 访问它。但在此之前,我想通过 Use 处理请求。如何访问数据库里面的Use?

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<UserContext>();

builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(policy => { policy.WithOrigins("*"); });
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseCors();

app.Use(async (context, next) =>
{
    // Здесь должно быть обращение к БД

    await next.Invoke(context);
});


app.MapGet("/vk/main_info", async (int vk_user_id, UserContext database) =>
{
    // Тут дальнейшая обработка запроса
});
    

app.Run();
asp.net
  • 0 个回答
  • 0 Views
Martin Hope
user511657
Asked: 2022-08-03 06:48:11 +0000 UTC

如何从 Ms Sql 添加和上传图像

  • 0

我有一个包含图片列的数据库。如何将图片插入数据库,然后通过什么类型的数据上传到Asp .net 控制器,再上传到angular。据我所知,这是一个 byte[] 数据类型,但这是在服务器端,但在 Angular 中呢?即使你把任何,那么图片根本不加载。

D B:D B

服务器端数据库模型:

public class Clothes

    {
        public int ClothesId { get; set; }
        
        public string VendorCode { get; set; }
        
        public string Name { get; set; }
        
        public byte[] Image { get; set; }
        
        public int Amount { get; set; }
        
        public int Price { get; set; }
        
    }

角度数据模型:

export class Clothes{
  vendorCode = "";

  name = "";

  image:any = [];

  amount = 0;

  price = 0;

}
sql-server asp.net
  • 1 个回答
  • 50 Views
Martin Hope
user511657
Asked: 2022-07-23 15:41:55 +0000 UTC

服务器响应错误:500 ASP.NET Core

  • 0

我想用angular和asp做一个服装店,但是我遇到了将数据发送到数据库的困难。有一个用于注册和授权的模块化表格。首先,我决定实现注册表单,但是发送数据时出错:在此处输入图像描述

我是新手,所以我附上了代码,因为我不知道错误的问题是什么。

用户.ts:

export class Users{
   id? = "";

   phone = "";

   email = "";

   fio = "";

   address ="";

   password = "";

}

用户服务.ts:

@Injectable({
  providedIn: 'root'
})
export class UserServiceService {

  private url = "User";

  constructor(private http: HttpClient) {
  }
  @Input() public createUser(users: Users): Observable<Users> {
    return this.http.post<Users>(
      `${environment.apiUrl}/${this.url}`,
      users
    );

导航菜单.ts:

export class NavMenuComponent {

  user:Users =new Users;
  @Output() userUpdated = new EventEmitter<Users>();
  constructor(private superHeroService: UserServiceService) {}
  createnewUser(users: Users) {
    this.superHeroService
      .createUser(users)
      .subscribe((users: Users) => this.userUpdated.emit(users));
  }

}

从输入中读取变量:

<div class="modal-body">
  <input [(ngModel)]="user.email" class="form-control">
  <input [(ngModel)]="user.phone" class="form-control">
  <input [(ngModel)]="user.address" class="form-control">
  <input [(ngModel)]="user.fio" class="form-control">
  <input type="password" [(ngModel)]="user.password" class="form-control"> :
  <input type="password" class="form-control">
</div>

后端:

用户控制器.cs:

[Route("api/[controller]")]
[ApiController]
public class UserController : Controller
{
    private readonly DataContext _context;

    public UserController(DataContext context)
    {
        _context = context;
    }
    [HttpPost]
        public async Task<IActionResult> CreateUser(User user)
        {
            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return Ok(await _context.Users.ToListAsync());
        }
}

数据上下文.cs:

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {
        Database.EnsureCreated();
    }

    public DbSet<User> Users => Set<User>();

}

User.cs:公共类用户

{
    public string? Id = string.Empty;

    public string Phone = string.Empty;

    public string Email = string.Empty;

    public string FIO = string.Empty;

    public string Password = string.Empty;

}
asp.net-mvc asp.net
  • 1 个回答
  • 49 Views
Martin Hope
Nik 25
Asked: 2022-07-20 18:59:55 +0000 UTC

我不知道如何在 EntityFramework 中绑定实体

  • 0

告诉我如何连接这两个实体,以便在数据库中创建 2 个相关表。我读了一篇关于 metanit 的文章,我无法理解。您收到InvalidOperationException :无法添加实体类型“Employee”的种子实体,因为没有为所需的属性“DepartmentId”提供值。

public class Department 
{
    [Key]
    public int DepartmentId { get; set; } 
    public string Name { get; set; } = null!;
    

}
public class Employee
{
     [Key]
    public int EmployeeId { get; set; }
    public string FirstName { get; set; } = null!;
    public string LastName { get; set; } = null!;
    public string DateOfBirth { get; set; } = null!;
    public string Adress { get; set; } = null!;
    public int DepartmentId { get; set; }
    [ForeignKey("DepartmentId")]
    public Department Department { get; set; } = null!;
    public string? Description { get; set; }

}

public class AppDbContext : DbContext, IDbContext
{
    public DbSet<Employee> Employees { get; set; } = null!;
    public DbSet<Department> Departments { get; set; } = null!;

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
        Database.EnsureCreated();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
            modelBuilder.Entity<Department>().HasData(
            new Department { DepartmentId = 1, Name = "Dev" },
            new Department { DepartmentId = 2, Name = "Marketing" },
            new Department { DepartmentId = 3, Name = "Art" });
        
            modelBuilder.Entity<Employee>().HasData(
            new Employee { Id = 1, FirstName = "Максим", LastName = "Максимов",Department = new(1,"DepName1"), DateOfBirth = "22.11.1990", Adress = "Novosibirsk", Description = "" },
            new Employee { Id = 2, FirstName = "Иван", LastName = "Иванов", Department = new(2, "DepName2"), DateOfBirth = "12.02.1996", Adress = "SPB", Description = ""},
            new Employee { Id = 3, FirstName = "Дмитрий", LastName = "Дмитриев", Department = new(3, "DepName3"), DateOfBirth = "10.06.1998",Adress = "MSK", Description = ""});
       
    }

   
}
c# asp.net
  • 2 个回答
  • 79 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5