thảo luận [.NET] Topic thảo luận các vấn đề xoay quanh .Net

  • Người tạo chủ đề Người tạo chủ đề pi nguyen
  • Ngày bắt đầu Ngày bắt đầu
Em chưa hiểu lắm, bác có thể quote lại cụ thể đoạn đó giúp em được không ạ

C#:
private bool verifyAccount(LoginRequest request) {
        SecurityConfiguration securityConfiguration = new SecurityConfiguration();
        string username = request.Username;
        string password = request.Password;
        // string passwordEncoded = securityConfiguration.encodePassword(request.Password)[1];
        FormattableString sql = $"SELECT * FROM Students WHERE Username = {username}";
        var student = context.Students.FromSql(sql).ToList();

        return (student != null) ? true : false;
    }
:beat_brick: tự gạch, ef core nó tự tham số hóa luôn chứ không phải nối chuỗi như mình thấy
https://www.learnentityframeworkcore.com/raw-sql/from-sql
 
:beat_brick: tự gạch, ef core nó tự tham số hóa luôn chứ không phải nối chuỗi như mình thấy
https://www.learnentityframeworkcore.com/raw-sql/from-sql
Nó bị lỗi như này bác ạ. Em cũng không rõ nó do cái gì

Mã:
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'Demo.Data.DataContext'.
      System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
         at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
         at Microsoft.Data.SqlClient.SqlBuffer.get_String()
         at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i)
         at lambda_method7(Closure , QueryContext , DbDataReader , Int32[] )
         at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()
      System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
         at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
         at Microsoft.Data.SqlClient.SqlBuffer.get_String()
         at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i)
         at lambda_method7(Closure , QueryContext , DbDataReader , Int32[] )
         at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()
 
Nó bị lỗi như này bác ạ. Em cũng không rõ nó do cái gì

Mã:
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'Demo.Data.DataContext'.
      System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
         at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
         at Microsoft.Data.SqlClient.SqlBuffer.get_String()
         at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i)
         at lambda_method7(Closure , QueryContext , DbDataReader , Int32[] )
         at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()
      System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
         at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
         at Microsoft.Data.SqlClient.SqlBuffer.get_String()
         at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i)
         at lambda_method7(Closure , QueryContext , DbDataReader , Int32[] )
         at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()
Có thể do một column nào của table Students trong db bị null, trong khi field tương ứng trong Student model được config là Required
 
Xài song song ef6 với dapper cho tối ưu tốc độ nha fen.
Cấu hình dễ lắm không khó đâu.
Ơ bên .Net này dùng đa dạng ORM vậy bác. Bên Java em thấy chủ yếu dùng Hibernate hoặc thậm chí chỉ JPA thôi ấy. Em mới chuyển qua .net để làm đồ án hết môn thôi :D
 
Tại vì EFcore của Microsoft lởm, Dapper tốc độ ngon hơn nhưng full feature mất tiền
Code first, migrations dựa trên class, ...
Nhiều cái đỡ mất thời gian lắm.
Dapper là database first mà fen.
Cái nào có lợi thế thời gian và ưu điểm thì mình tận dụng thôi.
Ai rảnh đâu mà tối ngày đi viết mấy câu sql cho 1 table. (dùng efcore code phát 1 nhanh lẹ)
Nào join bảng rồi phân trang, truy vấn phức tạp mới lấy dapper ra dùng thôi.
Efcore insert,update,delete nhanh hơn dapper.
Dapper nhanh hơn efcore ở query.
Fen có thể dùng lib benchmark để test.
Dẫn chứng: Cách utuber test dapper vs efcore speed
 
Vc insert lại dữ liệu. Xem lại cái nào cần not null cái nào cần null thì set cho đúng. Chẳng lẻ sau này bị lỗi này lại đi insert lại dữ liệu hả

Hihi cảm ơn bác ạ. Mấy cái fields trong class em toàn để default hết, chưa biết mấy cái annotation nó hoạt động như nào nên hơi lú

Sent from Xiaomi redmi note 4x using vozFApp
 
Khi nào dùng record khi nào dùng class nhỉ các thím
khi thím muốn
  • so sánh Equals giữa 2 reference object mà ko cần override == hay xài reference equals
  • read only (1 way flow) object, ví dụ như api parameter, event parameter
Record và class ở đây nghĩa là gì bạn.
Cho xin link tài liệu được không ?
Mình không rõ bạn đang nói cái gì.
record là feature mới của C# 9, bạn xem thêm ở đây nha https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/records
 
C# 9 có vẻ tiến hóa tốt như tụi kotlin hay swift rồi đó.
Cái immutable này tụi reactjs reducer hay redux saga đều dùng cái này để transfer dữ liệu.
Nói chung cũng biết cách học cái mới của thiên hạ để hút khách.

Phỏng vấn người ta cũng hay hỏi về immutable model đó nha mấy fen.
Các fen nên chú ý để còn phỏng vấn cho tốt.
 
Mọi người cho em hỏi là logic để nó validate cái token truyền vào là như nào vậy ạ. Em làm theo một số hướng dẫn thì thấy họ chỉ config như này là xong rồi. Em không rõ nó xử lý phần validate ở đâu nữa


C#:
builder.Services.AddAuthentication(options => {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o => {
    o.SaveToken = true;
    o.RequireHttpsMetadata = false;
    o.TokenValidationParameters = new TokenValidationParameters {
        ValidIssuer = builder.Configuration["Jwt:Issuer"],
        ValidAudience = builder.Configuration["Jwt:Audience"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true
    };
});

builder.Services.AddAuthorization();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();
 
Có thật là làm .NET thì sẽ chủ yếu làm cho các thị trường Âu Mỹ không vậy các bác ?
 
dotnet chỗ nào cũng chơi nha.
M$ vẫn là thế lực không thể thay thế.
M$ vẫn đang tiến hóa dotnet.
Tương lai dotnet có các tính năng như các ngôn ngữ khác là điều tất yếu.
 
Mọi người cho em hỏi là logic để nó validate cái token truyền vào là như nào vậy ạ. Em làm theo một số hướng dẫn thì thấy họ chỉ config như này là xong rồi. Em không rõ nó xử lý phần validate ở đâu nữa


C#:
builder.Services.AddAuthentication(options => {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o => {
    o.SaveToken = true;
    o.RequireHttpsMetadata = false;
    o.TokenValidationParameters = new TokenValidationParameters {
        ValidIssuer = builder.Configuration["Jwt:Issuer"],
        ValidAudience = builder.Configuration["Jwt:Audience"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true
    };
});

builder.Services.AddAuthorization();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();
validate nó nằm trong lib.
Bạn có thể lên github của dotnet mà xem nó làm cái gì trong đó.
Keyword middleware dotnet core api.
 

Thống kê chủ đề

Ngày tạo
pi nguyen,
Người trả lời cuối
keandanh123000,
Trả lời
2.024
Lượt xem
267.336
Quay lại
Lên đầu trang