Class Diagram for Social Media Platform - oop

In a professional social network, how would I represent connections between users? (like Linkedin) Should I create a connection class for which there would be an instance for every connection between 2 users or is that redundant? Should the user class instead have a self-association (reflexive association)?

Your User class would have a collection of Followings:
public class User
{
// ... the other code is omitted for the brevity
public IEnumerable<User> Followings { get; set; }
}
So if your database has the Following table:
CREATE TABLE Followings(
User_Id INT NOT NULL,
Following_Id INT NOT NULL,
DateCreated DATE NOT NULL,
);
Do not forget to create constraints and foreign keys in your table. Then it is possible to have Following class:
public class Followings {
public UserId int { get; set; }
public FollowingId int { get; set; }
public DateCreated DateTime { get; set; }
}
and then you can write easily the following query:
select * from Following where UserId = x.id -- or vice versa

Related

How to add foreign key in a model using ASP.NET ( MVC6 framework)

I am new to ASP.NET and trying out the new MVC 6 framework. i want to create an To do list App. After user authentication, I am redirecting to a dashboard page where I want to display to do tasks. I have created a model Task, but i am unsure about how to add a Foreign Key for the user id so that when i redirect the user to the dashboard page I can query based on the user's id.
public class Task
{
public int Id { get; set; }
// i want to add a fk to aspnetuser id here
}
Thank you for your help.
public class Task
{
public int Id { get; set; }
public int AnyNameForId { get; set; }
[ForeignKey("AnyNameForId")]
public AspnetUser User { get; set; }
}
public class AspnetUser
{
public int Id { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
}
suppose u have class XYZ having Id as primary key.
now u want to class XYZ's Id as foreign key in class ABC,
So, it can be done like writing the following in class ABC
public Guid XYZId{get;set;}
public XYZ xyz{get;set;}`
xyz is an object of class XYZ.
.net core will automatically take XYZId as fk in class ABC.....PLZ note that Id Should be the primary in class XYZ

How to Create index on component property

I have the following classes:
public class Event
{
public virtual Guid Id { get; set; }
public virtual string UserName { get; set; }
public virtual EventId EventId { get; set; }
}
public class EventId
{
public virtual Guid EventGuid {get; private set;}
}
I am using a fluent NHibernate automapping in order to map my classes and I override specific properties when needed.
Class: Event is in s_typesToMap and class: EventId is in s_components.
As a result I get the following table generated:
create table "Event" (
Id UNIQUEIDENTIFIER not null,
UserName NVARCHAR(255) null,
EventIdentifierEventGuid UNIQUEIDENTIFIER null,
primary key (Id)
)
I want to create an index on EventIdentifierEventGuid which is a property in Event component.
I tried to do it as follows:
.Override<Event>(obj => obj.Map(x => x.EventId.EventGuid).Index("EventId_index"))
When I generate the ddl I get the following:
create index EventId_index on "Event" (EventGuid)
The expected result should be an index on EventIdentifierEventGuid instead of EventGuid
How can I do it?

Entity Framework Code First - using UserId key of Memberships/Users table as Foreignkey in a custom table

I am trying to create a SQL table to include additional user information.
I want it to be created by Migrations in VS2012 (package console => update-database).
I see that there are two tables using UserId column as key: Memberships and Users tables.
I am trying to define the following class and map it to SQL table via Migrator:
[Key]
[Column(Order = 0)]
[ForeignKey("User")]
public Guid **UserId** { get; set; }
[Key]
[Column(Order = 1)]
[ForeignKey("Category")]
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
**public virtual User User { get; set; }**
Although it is obvious in the Users SQL table that UserId column is the key, I get this error message:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'User' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Users' is based on type 'User' that has no keys defined.
What I am missing here? Might be that Microsoft.VisualBasic.ApplicationServices.User / System.Web.Security.MembershipUser classes weren't necessarily mapped to the tables this way and vise versa, and therefore the UserId property is not declared is Key dataannotation?
I am open for other solutions for this problem.
Big Thanks!
I am currently using asp.net mvc4 with Azure db.
When I want to use UserId in other tables from UserProfile. Notice in AccountModels there is class UserProfile:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
Now, let's say your category is created by certain user, you can link category entry to user in following way.
[Table("Category")]
public class Category
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CategoryId { get; set; }
public UserProfile CreatorUserId { get; set; }
}
Otherwise, you can always use UserProfile as model.

How to collapse one-to-one parent-child tables into classes with Fluent NHibernate?

How do I map these existing tables to the classes below?
I have the following tables:
CREATE TABLE dbo.UserContact (
UserContactId int NOT NULL IDENTITY (1, 1),
UserId int NOT NULL,
ContactId int NOT NULL,
UserContactTypeId int NOT NULL,
FromDt datetime NULL,
ThruDt datetime NULL,
CreateDt datetime NOT NULL,
UpdateDt datetime NULL,
IsDeleted bit NULL,
CanSolicit bit NOT NULL
)
CREATE TABLE dbo.Contact (
ContactId int NOT NULL IDENTITY (1, 1),
CreateDt datetime NOT NULL,
UpdateDt datetime NULL
)
CREATE TABLE dbo.Electronic (
ContactId int NOT NULL,
URL nvarchar(512) NOT NULL,
ElectronicType smallint NULL
)
CREATE TABLE dbo.Phone (
ContactId int NOT NULL,
AreaCode nchar(3) NOT NULL,
PhoneNb nchar(7) NOT NULL,
Extension nchar(6) NULL,
PhoneType smallint NULL
)
CREATE TABLE dbo.Postal
(
ContactId int NOT NULL,
Street nvarchar(256) NOT NULL,
Specifier nvarchar(256) NULL,
GeocodeId int NULL
)
The tables Electronic, Phone and Postal are in a one-to-one relationship with Contact. The table UserContact is in a many-to-one with Contact; UserContact is an association table between User and Contact.
I also have the following classes:
public class Electronic : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string Url { get; set; }
public ElectronicType Type { get; set; }
}
public class Postal : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string Street { get; set; }
public string Specifier { get; set; }
public Geocode Geocode { get; set; }
}
public class Phone : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string AreaCode { get; set; }
public string PhoneNb { get; set; }
public string Extension { get; set; }
public PhoneType Type { get; set; }
}
public class UserContact : IntegerKeyEntity
{
private ICollection<Electronic> electronics = new HashSet<Electronic>();
private ICollection<Phone> phones = new HashSet<Phone>();
private ICollection<Postal> postals = new HashSet<Postal>();
// props
public virtual IEnumerable<Electronic> Electronics { get { return electronics; } }
public virtual IEnumerable<Phone> Phones { get { return phones; } }
public virtual IEnumerable<Postal> Postals { get { return postals; } }
}
So, I do I get from the four Contact tables (parent and child) down to the three classes? And, how do I map those three classes to the UserContact table. I'm assuming I can have three ILists, one for each class.
I think you're modeling this incorrectly. It appears to me that Electronic, Phone, and Postal extend (inherit from) Contact and this should be expressed in your domain model. Those classes are not related to Contact by one-to-one, they are concrete types that extend the abstract Contact type. If you model it this way you can map Contact using table-per-subclass inheritance mapping.
User would then have a many-to-many-relationship with Contact, and the user's Contacts collection would contain Contacts of any type.
Personally I would put all the Contact types into one table and use the simpler table-per-class mapping.

Self referencing a table

so I'm new to NHibernate and have a problem. Perhaps somebody can help me here.
Given a User-class with many, many properties:
public class User
{
public virtual Int64 Id { get; private set; }
public virtual string Firstname { get; set; }
public virtual string Lastname { get; set; }
public virtual string Username { get; set; }
public virtual string Email { get; set; }
...
public virtual string Comment { get; set; }
public virtual UserInfo LastModifiedBy { get; set; }
}
Here some DDL for the table:
CREATE TABLE USERS
(
"ID" BIGINT NOT NULL ,
"FIRSTNAME" VARCHAR(50) NOT NULL ,
"LASTNAME" VARCHAR(50) NOT NULL ,
"USERNAME" VARCHAR(128) NOT NULL ,
"EMAIL" VARCHAR(128) NOT NULL ,
...
"LASTMODIFIEDBY" BIGINT NOT NULL ,
) IN "USERSPACE1" ;
Database-table-field 'LASTMODIFIEDBY' holds for auditing purposes the Id from the User who is acting in case of inserts or updates. This would normally be an admin.
Because the UI shall display not this Int64 but admins name (pattern like 'Lastname, Firstname') I need to retrieve these values by self referencing table USERS to itself. Next is, that a whole object of type User would be overkill by the amount of unwanted fields. So there is a class UserInfo with much smaller footprint.
public class UserInfo
{
public Int64 Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string FullnameReverse
{
get { return string.Format("{0}, {1}", Lastname ?? string.Empty, Firstname ?? string.Empty); }
}
}
So here starts the problem. Actually I have no clue how to accomplish this task. Im not sure if I also must provide a mapping for class UserInfo and not only for class User. I'd like to integrate class UserInfo as Composite-element within the mapping for User-class. But I dont no how to define the mapping between USERS.ID and USERS.LASTMODIFIEDBY table-fields.
Hopefully I decribes my problem clear enough to get some hints. Thanks alot!
This looks like a bad case or premature optimization.
Are you really having any performance issues if you define LastModifiedBy as User and use <many-to-one> to map it?
That should be your starting point.