All,
I have read this article:
How AspNet Identity with my model
where the ApplicationUser has an additing property of
public int AddressId { get; set; }
that is a new property on the ApplicationUser.
But what I am wondering is what if I have a custom entity of my own and I want it to have a property that relates to the Application user:
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public ApplicationUser CurrentlyBorrowedBy { get; set; }
}
or
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public Guid CurrentlyBorrowedBy { get; set; }
}
The reason I might want to do this is so I can call a method like GetAllBooksBorrowedForUser(userid) for example.
Do I set the properties type to ApplicationUser as show above
or
use a Guid because the DataType of the Id on ApplicationUser is a Guid
or
is this the completely wrong way to do it?
All suggestions welcome.
Note: this is just psuedo code as I just want an understanding of this before I dive into my project.
thanks
Russ
From what I know, it should be:
public int CurrentlyBorrowedByID { get; set; }
public virtual ApplicationUser CurrentlyBorrowedBy { get; set; }
Where the ApplicationUser instance allows you to easily navigate
All,
Of course after finishing writing my question I found the answer right in front of me.
The sample and blog post written by pranav rastogi here:
http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx
explains how do do what I am talking about.
Still, feel tree to comment as there is more than one way to skin a cat.
thanks
Russ
Related
In my project, users can like a comment or a post, similar to Facebook likes. For this purpose, I created an abstract base class called Like keeping the if of the user who liked the post or the comment, and the datetime of the action. I have CommentLike class inheriting from Like class, which only have Comment property, and similarly, I have PostLike class inheriting from Like class, which only have Post property.
I use Entity Framework, and my database is generated successfully. In the generated database, there are CommentId, PostId, and Discriminator fields in the table. The thing is, if users like only one comment, and like only posts, then the CommentId field in the database will stay as null redundantly. I wonder if this is a problem. What if instead of these three fields, I only have one field, called RelatedItem which can indicate the associated Comment or the Post. In this case I can not define foreignkey I guess. Is this also a problem?
What would be best approach in such a situation?
public abstract class Like
{
public int LikeId { get; set; }
[Required]
public DateTime WhenLiked { get; set; }
[ForeignKey("WhoLiked")]
public string UserId { get; set; }
public Person WhoLiked { get; set; }
}
public class PostLike : Like
{
[Required]
public Post Post { get; set; }
[ForeignKey("Post")]
public int PostId { get; set; }
}
public class CommentLike : Like
{
[ForeignKey("Comment")]
public int CommentId { get; set; }
[Required]
public Comment Comment { get; set; }
}
Here is the database generated:
I'm currently working on a blog written in asp.net mvc. I have a table that stores comments with the following model
public partial class Comment
{
public int CommentId { get; set; }
public string Comment { get; set; }
public System.DateTime CommentDate { get; set; }
public int AuthorId { get; set; }
public int PostId { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual Post Post { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
Currently the comment is still a one-level comment i.e a user cannot reply to a comment. How can i implement a multilevel comment system in such a way that a comment can have a reply and that reply can have another reply and so on.
Add parentCommentId column which refers to the Comments table.
After that in each reply :
If it is a direct reply to the post, leave parentCommentId column empty
If it is a reply to a previously posted comment, put that comment id in this column. In this case you can leave postid column empty or not. It depends on your favor!
first i need to excuse you for my bad English if you want a tree of comments as i understood like you comment and reply and you need to reply on my reply thats make u should add 1 more field you can call it subcomment id which it will be as an forign key to comment table " self relation " and this subcommenid will hold the parent comment ID
But essentially..
I don't know how your POST Class is defined.. Is it a collection? Or a simple class? If it's not a collection you want to make it as so..
Inside your Comment Class Change..
public virtual Post Post { get; set; }
to:
Public virtual ICollection<Post> Posts { get; set; }
or define it in another class private field..
This will defines the relationship and will be your navigation property as well..
So in your controller you can return both tables like such...
public ActionResult GetComments(int id)
{
db.context.comments.Include("Posts").Where(c => c.CommentID == id).ToList();
}
You need create custom Htmlhelper / TreeView and pass data in to this helper. Data model will be according to Mahmoud Moravej.
I'm new to ASP.Net Identity, and I'm looking for a good tutorial for using Identity in conjunction with other classes in my model.
As an example I have a basic Ratings class that looks like this (from a project not using Identity)
public class Rating
{
public int Id { get; set; }
public Product Product { get; set; }
public User User { get; set; }
public int Stars { get; set; }
public string Comment { get; set; }
public bool Active { get; set; }
}
And a User Class that looks a bit like this
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public ICollection<Rating> Ratings { get; set; }
}
Looking for a way to achieve the same with Identity. My test project is set up with MVC5, and code first
The recommended solution is to add the properties you need to the ApplicationUser class
You can also use your own "User table", in your case that would be the User class. You'd have to inherit from IdentityUser.
This article has examples of how to do both.
I agree with Rui.
Here is a site that will teach you How to Extend Identity Accounts and also Implement Based Authentication. When I was starting with Identity, that site taught me a lot.
As a related hint: Watch out when you implement a Unit of Work pattern in your project. ASP.NET identities datacontext needs to be the same as the Uow datacontext, otherwise the whole think will crash.
A good starting point may be this: ASP.NET Identity with Repository and Unit of Work
I have a class that has two references to one class (User):
public class Xpto {
public string Username { get; set; }
public virtual User User { get; set; }
public string Username2 { get; set; }
public virtual User User2 { get; set; }
}
The thing is EF only creates references to the first key (Username). That way User and User2 have Username as key and not what I intended...
I found this to be the answer:
nHibernate, mapping two properties to the same class
But I wouldn't know how to apply this to my scenario.
Thanks.
EDIT: Folks, nevermind... I guess I should've looked a little further.
The answer is here:
How do I create a POCO object that has 2 references to another class
The standard is <property_name><key_name>
So the correct way would be UserUsername and User2Username
Thanks.
Consider using the ForeignKeyAttribute instead, then you can select the names you like for your key attributes.
public class Xpto {
[ForeignKey("User")]
public string Username { get; set; }
public virtual User User { get; set; }
[ForeignKey("User2")]
public string Username2 { get; set; }
public virtual User User2 { get; set; }
}
I an developing a page where users will be able to add and modify existing content, its not a wiki per sé but sort of, like SO's editing abilities.
I am working with EF4 and the new Code First approach in the latest CTP, so what would be the best class design for this?
my current guess is something like this:
public class VersionableText
{
public int Id { get; set; }
public DateTime Date{ get; set; }
public String Text{ get; set; }
public virtual User User{ get; set; }
}
and then use it in my other entities, in a SO context it could be something like this
public class Question
{
public int Id {get; set;}
public virtual VersionableText Title {get; set;}
public virtual VersionableText Content{get; set;}
...
}
But I'm not really convinced by it.. since I am also going to have tags, ability to delete/undelete posts, rollback, etc. Do you know how to properly design classes that help me version the content properly?
Aim for simplicity
The main question that you need to ask yourself is Are you going to show all versions all the time or the latest version most of the time and all of them on request? Similar to here. Most of the time you only see the latest version.
If this is the same with our case I wouldn't care so much about these versions. But when you'd want to show them all on one page class design more or less depends on the way that you'd like to show it. Is it going to be showing changes and things like that.
I'd rather have a class like:
public class Question
{
public int Id { get; set; }
public QuestionStatus Status { get; set; }
}
public class QuestionHistory
{
public Question Question { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public User Author { get; set; }
public DateTime Created { get; set; }
public IList<Tag> Tags { get; set; }
}
And when I'd display all of them I'd just return a list of these ordered by LastChange. I've added tags list but I didn't add any of the other process-related properties related to question state. It hugely depends on the process sequence.