Pagano
Pagano
CC#
Created by Pagano on 7/12/2024 in #help
Custom Made Collection
I guess this is a real stupid question but how do I create my own custom collection? I have a Player class and I need to create a custom class that is a Collection of Player with other fields and methods but how do I implement my own collection in .NET Core? If I had to guess, I would say implement IEnumerable<T> and IEnumerator<T> but I don't know how to implement them either. Can I have some help please? I can provide with a more in-depth of the idea and my classes if needed.
103 replies
CC#
Created by Pagano on 3/19/2024 in #help
Entity Framework Core Issue - Many to Many relationship
I got this error now:
Unable to create a 'DbContext' of type ''. The exception 'The 'Player' property 'PlayerMatch.Player' could not be mapped because the database provider does not support this type. Consider converting the property value to a type supported by the database using a value converter. See https://aka.ms/efcore-docs-value-converters for more information. Alternately, exclude the property from the model using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Unable to create a 'DbContext' of type ''. The exception 'The 'Player' property 'PlayerMatch.Player' could not be mapped because the database provider does not support this type. Consider converting the property value to a type supported by the database using a value converter. See https://aka.ms/efcore-docs-value-converters for more information. Alternately, exclude the property from the model using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
2 replies
CC#
Created by Pagano on 3/19/2024 in #help
Beginner in EF Core and SQL Server in need for help
Hello, I am new here but I needed some help using my project. I need to connect to a SQL Server DB using .NET Core with EF Core. This is a simple example of my problem:
internal class UserModel
{
[Key]
[Column("ID")]
[Required]
public int Id { get; set; }
[Column("Discord ID")]
[Required]
public long DiscordId { get; set; }
[Column("Discord Name")]
[Required]
[MaxLength(32)]
public required string DiscordName { get; set; }
}
internal class UserModel
{
[Key]
[Column("ID")]
[Required]
public int Id { get; set; }
[Column("Discord ID")]
[Required]
public long DiscordId { get; set; }
[Column("Discord Name")]
[Required]
[MaxLength(32)]
public required string DiscordName { get; set; }
}
This would be the table mapped to a C# class, right? However, I need to work with User objects to perform methods and have fields that need to have private setters and getters etc. How would I do that? I was told to user another class like this to wrap around UserModel:
internal class User(UserModel model)
{
public int Id { get; private set; } = model.Id;
public long DiscordId { get; private set; } = model.DiscordId;
public string DiscordName { get; private set; } = model.DiscordName;
// Methods
}
internal class User(UserModel model)
{
public int Id { get; private set; } = model.Id;
public long DiscordId { get; private set; } = model.DiscordId;
public string DiscordName { get; private set; } = model.DiscordName;
// Methods
}
But I ended up in an unsolved problem. When using my code I will work with User objects, right? But I have some questions surrounding this idea (if the best - correct me please if there is any better way).
1) How do I create an instance of User if I don't have a UserModel object? 2) When adding a new UserModel to the DB, how do I do it if I am using User objects throughtout the project, not UserModel objects?
Probably dumb questions but basically, how I keep alternating between their uses? There is probably a better way to do it. If so, please I want to do this the best way possible. I am new to EF Core and SQL Server in general but willing to learn anything to make my project the best version possible.
5 replies