Data model for User Notifications of many Types - entity-framework

I am trying to figure out how to model notifications. This is what I tried.
public class NotificationType
{
public int NotificationTypeID { get; set; }
public string Type { get; set; }
public string Action { get; set; }
}
public class Notification
{
public int NotificationID { get; set; }
public bool ReadStatus { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("User")]
public int UserID { get; set; }
[ForeignKey("NotificationType")]
public int NotificationTypeID { get; set; }
public int CommentID { get; set; }
public int ProjectID { get; set; }
public Comment Comment { get; set; }
public Project Project { get; set; }
public NotificationType NotificationType { get; set; }
public User User { get; set; }
}
Frankly, I feel like I have no idea what I am doing still, but let me tell you what I was trying to do, if it's a bad idea, tell me, if not please tell me how I do it.
I have Notifications that are for actions that occur related to a Comment, Reply and Project - That's why I have all those navigation properties and fields. I want to basically use the NotificationType to determine what it is and then use the appropriate ID field to get the information to display the notification to a user.
First problem, I can't seem to make those ID (CommentID ProjectID ) fields Nullable so I don't always have to have them.
How to I make them nullable using data annotations and or the fluent api OR design my model better?

Make them nullable properties
public class Notification
{
public int NotificationID { get; set; }
public bool ReadStatus { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("User")]
public int UserID { get; set; }
[ForeignKey("NotificationType")]
public int NotificationTypeID { get; set; }
public int? CommentID { get; set; }
public int? ProjectID { get; set; }
public Comment Comment { get; set; }
public Project Project { get; set; }
public NotificationType NotificationType { get; set; }
public User User { get; set; }
}
Then use the fluent API
modelBuilder.Entity<Notification>().HasOptional(n => n.Comment).WithMany()
.HasForeignKey(n => n.CommentID);

I found this a kind of complex but complete notification system : http://library.blackboard.com/ref/df5b20ed-ce8d-4428-a595-a0091b23dda3/Content/Mini_TOCs/mt_admin_app_system_manage_comm_notify_frame.htm
I think that many good ideas can be borrowed from it.

Related

Load related entities with a single .Include() in Entity Framework?

Is there a better way to load all the related entities?
Below is the ScholarshipRequest class which also has Scholarship, Status, Student, Program and User.
public class ScholarshipRequest
{
public int Id { get; set; }
public int Year { get; set; }
public Status Status { get; set; }
public DateTime ApplicationDate { get; set; }
public DateTime ActionDate { get; set; }
public Scholarship Scholarship { get; set; }
public Program Program { get; set; }
public Student Student { get; set; }
public User User { get; set; }
}
I am just posting Scholarship class here, rest are similar.
public class Scholarship
{
public int Id { get; set; }
public string Name { get; set; }
}
The below code works fine but is there a better way where i can use a single .Include() to load them all or may be some other way?
ScholarshipRequestRepository repo = new ScholarshipRequestRepository(dBContext);
List<ScholarshipRequest> stdList = repo.Collection()
.Include("Status").Include("Student").Include("User").Include("Scholarship")
.Where(x => x.User.Id == userId).ToList();

Code first scaffolding .net core 3.1 not working correctly

I have 2 classes that I have defined in my .net core entity framework 3.1 application
Room and RoomTypes
public class Room
{
public int RoomId { get; set; }
[StringLength(250)]
public string Roomname { get; set; }
[StringLength(450)]
public string RoomDescription { get; set; }
[StringLength(50)]
public string Rate { get; set; }
[StringLength(50)]
public string RateSpecial { get; set; }
public bool? Available { get; set; }
[StringLength(350)]
public string PhotoPath { get; set; }
public bool? Internet { get; set; }
public bool? TVSet { get; set; }
public bool? ElectronicSafe { get; set; }
public bool? TeaCoffee { get; set; }
public bool? Linnin { get; set; }
public int? RoomTypeId { get; set; }
public virtual RoomType RoomType { get; set; }
}
and RoomTypes
public class RoomType
{
public int RoomTypeId { get; set; }
[Column("RoomType")]
[StringLength(90)]
public string RoomType1 { get; set; }
public virtual ICollection<Room> Rooms { get; set; }
}
But when I scaffold my models, the lookup field (RoomType) shows the ID field and not the type.
If I scaffold the exact same in MVC 5, it does it correctly and the lookup field shows type and not the ID.
Can anyone shed light on this issue ??
Thanks
Luke
I solved the issue: One of my columns name was the same as the table!!
public virtual RoomType RoomType { get; set; } and one of my fields was roomtype.
Hope this prevents someone from going through the misery, at first very hard to spot.
Happy days!
P.S. With MVC 5 scaffolding, it prevents error, it just renames the field by adding a 1. This does not happen in .net core.

Map many to many objects using Entity Framework

For example we have profile and organisation. Both have articles.
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
}
public class Profile
{
public int Id { get; set; }
public string Email { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
public class Organisation
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
In this way Article should have two kinds of parent so it should have something like parent type to be able to access a parent when you select articles directly.
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public int ParentId { get; set; }
public ArticleParentType Parent { get; set; }
}
Is it possible to map it using Entity Framework?
Is it a good idea to do it?
What is the best practice for storing this kind of data?
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public int ParentId { get; set; }
public ArticleParentType Parent { get; set; }
}
Is it possible to map it using Entity Framework?
Is it a good idea to do it?
Possible yes but not a good idea. The underlying Database can't use a foreign key for Parentid. It would be slow.
What is the best practice for storing this kind of data?
A simple approach, with 2 Nullable parents and without CascadeOnDelete:
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public virtual Profile Profile { get; set; }
public virtual Organisation Organisation { get; set; }
}
Alternatively you could use inheritance for Article, ie class OrganisationArticle : Article {}

Entity framework foreign key

I have 2 classes
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
and
public class PersonWebsite
{
public int Id { get; set; }
public string Website { get; set; }
public int PersonId{ get; set; }
}
I've seen stuff like this being done before
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public ICollection<PersonWebsite> PersonWebsites{ get; set; }
}
How could I go about implementing the code that when a Person is initialized, the PersonWebsites List will automatically be initialised and get all the PersonWebsite objects that have the same PersonId as the class that calls it.
Lazy Loading:
You can make PersonWebsites property virtual:
public virtual ICollection<PersonWebsite> PersonWebsites{ get; set; }
Entity framework will load it from the database as soon as it's required.
Also this method requires you to have lazy loading enabled which is by default:
DbContext.ContextOptions.LazyLoadingEnabled = true;
Eager Loading:
You can use include to force entity framework to load PersonWebsites on the first query:
DbSet.Include(p => p.PersonWebsites);
You may also want to change your PersonWebsite class like this, in order to navigate to the person from the PersonWebsite object (using Lazy-Loading):
public class PersonWebsite
{
public int Id { get; set; }
public string Website { get; set; }
[ForeignKey("Person")]
public int PersonId{ get; set; }
public virtual Person Person {get;set;}
}

ADO.NET EF what is the purpose of using List<> in the entities

Right now I'm learning ADO.NET Entity Framework and there's one thing that I can't explain to myself. Here is a source code from a tutorial I've been using recently:
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public User UserId { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string DisplayName { get; set; }
}
First I thought that the using of List<> is the way to implement Foreign Key-like behaviour but now knowing that's not the case why we need and for what purpose we use List<> in our entites?
To show that Blog have a lot of Posts, when you will build your project in DB will be the relation 1xBlog--->NxPost where N=unlimited. This will show that each Blog can have unlimited amount of Posts