How to connect AspNetUsers with another table? - entity-framework

Hi there StackOverflow people.
I currently have a table with clients:
public class Cliente
{
public Cliente()
{
}
[Key]
public int IDCliente { get; set; }
[Required]
public string Nome { get; set; }
[Required]
public string Morada { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string UserName { get; set; }
}
}
And I have the AspNetUsers table...
Also, this is my client table data.
I have that UserName on the table Cliente, and I want to connect it to the aspnetUsers, so that when I create a user, that it will automatically add information between both automatically.
How can that be done?
Thanks in advance.

You can customize your own UserManager which is inheriting base UserManager , then add Cliente in override CreateUser function implementation.

Related

PostgreSQL Entity Framework Code First Migration generating new table for object type JSONB field

I am using .net core 3.1
public class Person
{
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[Required]
public DateTime DateOfBirth { get; set; }
**[Column(TypeName = "jsonb")]**
public IList<Address> Addresses { get; set; }
}
public class Address
{
public string Type { get; set; }
public string Company { get; set; }
public string Number { get; set; }
public string Street { get; set; }
public string City { get; set; }
}
But, when I ran add migration command that generating new table for Addresses, that I don't want
I just want to capture address list in jsonb format but not in separate table.
I am using fluent api for configuration (just for understanding above class is decorated), do we have any property where I can specify to not generate new table for any object type property.
I tried - [NotMapped] but this ignore that specific column entirely
Thank you!

AspNetUsers as foreign key in another table

Now I am building a mini blog. when user create a post I want to insert user id from AspNetUsers in post table as a foreign key
here the post model, so can anyone tell me the steps to make it.
public class Post
{
[Required]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Content { get; set; }
[Required]
public string Path { get; set; }
[Required]
public DateTime PostDate { get; set; }
public ApplicationUser User { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
You have to use the below code to use AspNetUsersId as a foreign key. You can use [Required] if you don't want to make AspNetUsersId as nullable.
public AspNetUsers User { get; set; }
[ForeignKey("AspNetUsersId")]
[Required]
public int AspNetUsersId { get; set; }
Yes, you have to manually set the AspNetUsersId when you are inserting the record in Post table.
Lets say you have object of class Post as obj. So you have to write the below code in your method, if AspNetUsers table PK is Id.
obj.AspNetUsersId = AspNetUsers.Id;

Entity Framework Core error on Insert when exists a foreign key to a View

This is my model (semplified):
PRAT is the main table
public partial class PRAT
{
public int ID { get; set; }
public string PRATICA { get; set; }
public int ANNO { get; set; }
public string VARIANTE { get; set; }
[ForeignKey("ID")]
public VW_PRATICHE_CONTIPO VW_PRATICHE_CONTIPO { get; set; }
}
VW_PRATICHE_CONTIPO is a View (not a table!) in the database that contains some data related to PRAT table
public class VW_PRATICHE_CONTIPO
{
public int ID { get; set; }
public DateTime? DATAPRES { get; set; }
public string PROTGEN { get; set; }
public string TIPO { get; set; }
public string TIPOEXTRA { get; set; }
public string TIPOISTANZA { get; set; }
public string TIPOPRAT { get; set; }
}
The one-to-one relation between the table and the View is based on the ID field.
I need this because I want to do a query like this:
context.PRAT.Include(x=> x.VW_PRATICHE_CONTIPO)
This query works as exptected.
The problem happens when I try to save a new entity in PRAT.
When i do this:
context.PRAT.Add(prat);
await context.SaveChangesAsync();
I got this error:
The property 'ID' on entity type 'PRAT' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
If I remove the navigation property from PRAT all works fine, but I can't do the Include in my Query.
Can anybody help me?
Thank you.

Entity framework- EF Code First Select foreign key

Model:
public class Address
{
[Key]
public long AddressId { get; set; }
public string Street { get; set; }
public string Town { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
public class User
{
[Key]
public long UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public virtual List<Address> Addresses { get; set; }
}
DBContext:
public class DataModelContext : DbContext
{
public DbSet<Address> Addresses { get; set; }
public DbSet<User> Users{ get; set; }
}
Using above code its creating this schema for DB.
Addresses Users
----------- -------
AddressId(PK) UserId(PK)
Street UserName
Town Password
State
Country
User_UserId(FK)
Now i want to access User_UserId from Addresses table, but it not showing any property there. Its giving error "Address does not contain a definition for User_UserId.....
using (var db = new DataModelContext())
{
db.Addresses.Select(x=>x.User_UserId).ToList();
}
Use Foreign-Key Association instead of independant association while creating models. It means that, you must include a foreign key property in your model alongside with a corresponding Navigational Property. For example:
public class Address
{
...
public int UserId {get; set;} //Foreign-Key property
[ForeignKey("UserId")]
public virtual User User { get; set; } // Navigational Property
...
}
Read this article for more information.

updating an entity needs re-making relations in ef?

I am using ef 4.1 in my application and I have entities like below:
public partial class Role
{
[Key]
public int Id { get; set; }
[StringLength(20)]
[Required()]
public string RoleTitle { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public partial class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long UserId { get; set; }
[StringLength(20)]
[Required()]
public string UserName { get; set; }
public bool Status { get; set; }
[Required()]
public virtual Role Role { get; set; }
}
is it true that every time I want to update some field of User entity, say Status, I should re0make it is relations ?
Cause when I want to update only status field and save changes (I use Unit of Work), it throws and says "The Role field is required."...
No, you don't have to remake its relations. You also shouldn't be putting the Required annotation on the virtual property. You should be putting it on the ForeignKey ID field for the Role table. I believe you're getting the error because it's never setting the Role correctly on the User class in the first place, which is why you keep having to remake it.
To illustrate, here's what your User class should look like:
public partial class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long UserId { get; set; }
[StringLength(20)]
[Required]
public string UserName { get; set; }
[Required, ForeignKey("Role")]
public int RoleID { get; set; }
public bool Status { get; set; }
public virtual Role Role { get; set; }
}