Al3mid3x
Al3mid3x
Explore posts from servers
CC#
Created by Al3mid3x on 7/26/2024 in #help
Seeding Data Into Database
and they already have a default value of an empty array in the User.cs class
11 replies
CC#
Created by Al3mid3x on 7/26/2024 in #help
Seeding Data Into Database
but you can see that i have given them a value in the seed class
11 replies
CC#
Created by Al3mid3x on 7/26/2024 in #help
Seeding Data Into Database
the error is happining because of the PasswordHash and PasswordSalt are not modified in the json folder
11 replies
CC#
Created by Al3mid3x on 7/26/2024 in #help
Seeding Data Into Database
Seed Class
c#
using API.Entities;
using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;

namespace API.Data
{
public class Seed
{
public static async Task SeedUsers(DataContext context)
{
if (await context.Users.AnyAsync()) return;

var userData = await File.ReadAllTextAsync("Data/UserSeedData.json");

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

var users = JsonSerializer.Deserialize<List<User>>(userData, options);

if(users == null) return;

foreach (var user in users) {
using var hmac = new HMACSHA512();

user.UserName = user.UserName.ToLower();
user.PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes("Pa$$w0rd"));
user.PasswordSalt = hmac.Key;

context.Users.Add(user);

}
await context.SaveChangesAsync();
}
}
}
c#
using API.Entities;
using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;

namespace API.Data
{
public class Seed
{
public static async Task SeedUsers(DataContext context)
{
if (await context.Users.AnyAsync()) return;

var userData = await File.ReadAllTextAsync("Data/UserSeedData.json");

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

var users = JsonSerializer.Deserialize<List<User>>(userData, options);

if(users == null) return;

foreach (var user in users) {
using var hmac = new HMACSHA512();

user.UserName = user.UserName.ToLower();
user.PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes("Pa$$w0rd"));
user.PasswordSalt = hmac.Key;

context.Users.Add(user);

}
await context.SaveChangesAsync();
}
}
}
11 replies
CC#
Created by Al3mid3x on 7/26/2024 in #help
Seeding Data Into Database
Json data file
11 replies
CC#
Created by Al3mid3x on 7/26/2024 in #help
Seeding Data Into Database
Exception
fail: Program[0]
An error occurred during migration
System.Text.Json.JsonException: JSON deserialization for type 'API.Entities.User' was missing required properties, including the following: PasswordHash, PasswordSalt
at System.Text.Json.ThrowHelper.ThrowJsonException_JsonRequiredPropertyMissing(JsonTypeInfo parent, BitArray requiredPropertiesSet)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at API.Data.Seed.SeedUsers(DataContext context) in C:\Users\pc\source\repos\DatingApp\API\Data\Seed.cs:line 19
at Program.<Main>$(String[] args) in C:\Users\pc\source\repos\DatingApp\API\Program.cs:line 30
fail: Program[0]
An error occurred during migration
System.Text.Json.JsonException: JSON deserialization for type 'API.Entities.User' was missing required properties, including the following: PasswordHash, PasswordSalt
at System.Text.Json.ThrowHelper.ThrowJsonException_JsonRequiredPropertyMissing(JsonTypeInfo parent, BitArray requiredPropertiesSet)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at API.Data.Seed.SeedUsers(DataContext context) in C:\Users\pc\source\repos\DatingApp\API\Data\Seed.cs:line 19
at Program.<Main>$(String[] args) in C:\Users\pc\source\repos\DatingApp\API\Program.cs:line 30
11 replies
CC#
Created by Al3mid3x on 7/26/2024 in #help
Seeding Data Into Database
User:
c#
using API.Extensions;

namespace API.Entities
{
public class User
{
public int Id{ get; set; }

public required string UserName{ get; set; }
public required byte[] PasswordHash { get; set; } = [];
public required byte[] PasswordSalt { get; set; } = [];

public DateOnly DateOfBirth { get; set; }
public required string KnownAs{ get; set; }
public DateTime Created { get; set; } = DateTime.UtcNow;
public DateTime LastActive { get; set; } = DateTime.UtcNow;

public required string Gender { get; set; }
public string? Introduction { get; set; }
public string? Interests { get; set; }
public string? LookingFor { get; set; }

public required string City { get; set; }
public required string Country { get; set; }
public List<Photo> Photos { get; set; }

public int GetAge()
{
return DateOfBirth.CalculateAge();
}
}
}
c#
using API.Extensions;

namespace API.Entities
{
public class User
{
public int Id{ get; set; }

public required string UserName{ get; set; }
public required byte[] PasswordHash { get; set; } = [];
public required byte[] PasswordSalt { get; set; } = [];

public DateOnly DateOfBirth { get; set; }
public required string KnownAs{ get; set; }
public DateTime Created { get; set; } = DateTime.UtcNow;
public DateTime LastActive { get; set; } = DateTime.UtcNow;

public required string Gender { get; set; }
public string? Introduction { get; set; }
public string? Interests { get; set; }
public string? LookingFor { get; set; }

public required string City { get; set; }
public required string Country { get; set; }
public List<Photo> Photos { get; set; }

public int GetAge()
{
return DateOfBirth.CalculateAge();
}
}
}
11 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
No description
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
No description
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
No description
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
i'll send the latest update of the code if anybody else saw this post would know how i did fix it
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
Thank you all for helping i've been stuck on this one for a long time
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
it was a pleasure meeting you 😄
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
thats ok habibi
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
why didn't u tell me earlier it would've been easier for me to explain my problem xd
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
i'ma add you accept
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
oh really
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
ye i'm actually arab
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
Jordan
101 replies
CC#
Created by Al3mid3x on 6/29/2024 in #help
✅ Image input field is required
yes
101 replies