Integrate MVC Core Identity with other Database First Tables - asp.net-mvc

I am using MVC Core with the default individual user authentication.
I added FullName field to AspNetUsers Table and it is working fine.
I need to create a relationship between my tables to the id field in the AspNetUsers table so I can get the fullname of the user.
I have no idea on how to create relationship in Entity framework, I tried to add the following in the ApplicationUser Class
public class ApplicationUser : IdentityUser
{
public string FullName { get; set; }
public mytable T { get; set; }
}
and
in mytable model, I added the following
public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }
unfortunately, I am getting the following error:
The entity type 'IdentityUserLogin' requires a primary key to be defined
any idea?

The first issue solved by following link
https://forums.asp.net/post/6144294.aspx
the IdentityUserLogin' requires a primary key to be defined also solved by
https://stackoverflow.com/a/40824620/7046796

Related

Create a Many to Many relationship between ApplicationUser and custom table

I am a newbie to building website's in asp.net mvc5. I followed some tutorials and build my first website based on a existing database, so that's why i choose for database first approach.
Now I want to create with many to many relationship between the applicationuser object with my resource object.
from db point of view:
AspNetUsers <-> ApplicationUserResources <-> Resource
Which steps do I need to follow when using a database first approach to archive this relationship.
Wesley
You can add any navigation props in your ApplicationUser class as well as in any other EF Entity
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Resource> Resources{ get; set; }
}
public class Resource
{
public virtual ICollection<ApplicationUser > ApplicationUsers { get; set; }
}

Identity Server 4 user model relations

I am quite new to .Net Core and Identity Server 4, I followed several tutorials and got an Identity Server with Asp .Net Core Identity running.
Then I have a separate API project with restricted routes that works well.
So let's say I want users to be able to post comments to this api.
In my Identity Server project I have a model called "ApplicationUser" extending "IdentityUser".
public class ApplicationUser : IdentityUser
{
}
In my API project I have a "Comment" model:
public class Comment
{
public long Id { get; set; };
public string Text { get; set; };
}
Now I want to know which User posted the comment, so I added a foreign key to the user and a pointer to ApplicationUser in my Comment model
public class Comment
{
public long Id { get; set; };
public string Text { get; set; };
public long UserId { get; set; }
public ApplicationUser User { get; set; }
}
But since "ApplicationUser" is in my IdentityServer project, I need to add a Project dependency in my Api project pointing to the IdentityServer project.
Then in my "ApplicationUser" model I want to have a one-to-many relation to the comments posted by that User, like this:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Comment> Comments { get; set; }
}
But here I am encountering a problem since my "IdentityServer" project doesn't have access to my "Comment" model in the API project and if I try to add dependency to that project then I get an error of "circular dependency".
So I guess I am doing this the wrong way. How can I access users from the IdentityServer project in my API project models and vice-versa in the best way?
Thanks!

Tracking a user by GUID or by an integer in identity 2

As of Identity 2 they have switched from a id with an integer value (ex. 1,2,3,4,...) to a id with a nvarchar value that stores the id as some long string like
a234vt-23sdlj23klj-34jkh34jh34-23jk4jh2
If I'm creating an object that will have a single owner belonging to the person logged in, so I need to attach the user id to it, should I use this new id from Identity 2 or should I try and create some other value like an integer and put it into the aspnetusers table? Does it really matter, all I'm doing is fetching Gift object by owner(userid) and displaying/modifying gift objects on a form.
here is an example of my product object
public class Gift
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Category> Categories { get; set; }
public int Rating { get; set; }
public GiftStatus Status { get; set; }
Public string UserId {get; set; //where userid is the id of the user that owns this object, should it be 3kj23jh3-h3hk1jh2-khj2h34l1b-n22g35l ???
}
There is no problem with using the Guid UserId to reference a user, if that's what you're concerned about. Unless you have a specific reason for not wanting to use a Guid as the UserId, I would suggest you just use the default behavior in order to simplify implementation. Having two separate Ids to keep track of a user sounds needlessly complicated, I wouldn't recommend that path.
If you do have a good reason for requiring an Int as the primary key instead of a Guid, you might take a look at this: http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx (scroll down to the "Make the type of Primary Key be extensible for Users and Roles" section). This page has a link to an example project which shows you how to use an Int as the PK. It also mentions that this extension can be used to migrate applications which use Int PKs to the new Identity 2.0.
Here's another article that may be helpful: http://www.codeproject.com/Articles/777733/ASP-NET-Identity-Change-Primary-Key

Azure Mobile Services table to c# class mismatch: Error: Item identifiers can only be specified via the 'id' property

I've just refactored my Xamarin Android app that uses Azure Mobile Services as backend. Recently I got this really strange error.
When I try to insert a new item to the database:
_client.GetTable<T>().InsertAsync(instance)
I then get a MobileServiceInvalidOperationException that goes:
Error: Item identifiers can only be specified via the 'id' property.
It seems like AMS is not able to match the class properties with the sql table. If I change Id to id then the next issue is that it cannot match __version with my property Version.
However the strange part is that it all works if I insert or read from the table at app startup (which I did before the refactoring). And only one table has this issue.
Does anyone have a clue what it could be? I could do a workaround by reading once at startup, but that's just not satisfying.
Here is the class / table.
using Microsoft.WindowsAzure.MobileServices;
using System;
namespace Core.Models
{
public class PostAction
{
public string Id { get; set; }
public string UserId { get; set; }
public string ImageURL { get; set; }
[CreatedAt]
public DateTime CreatedAt { get; set; }
[UpdatedAt]
public DateTime UpdatedAt { get; set; }
[Version]
public string Version { get; set; }
}
}
Try to change the data type from string to int for Id property.
Azure uses strings instead of ints as PKs. I you have old data in your app it might work up to a certain point since SQLite is pretty tolerant in terms of types. BUT at some point eg. if you used foreign key relations you will have to migrate your database.

Insert values to entity framework in asp.net mvc4

In my asp.net mvc4 sample i have table in the name of Sample with three column as Name,Dept and Id.In this Id as identity and primary.I get a value for Name and Dept from user and insert that value to Sample table of entity framework.It pass value '0' to Id.And i got an error as "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries".Please help me.
set "autoincrement" on the column and set the id as primary key in sql server ..
In EF code-first this is done by data annotations. Make sure you recreate your table (or database) after changing the scheme.
public class Sample
{
[Key]
[Required]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] //auto increment id
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Dept { get; set; }
}
You don't need to pass an integer for Id. EF will resolve this automatically. Just create the object by filling in the name and the dept. By saving the changes to the database, EF will automatically fill in the auto generated Id.

Resources