First class aspect ratio

I am trying to create my model in the Entity Framework and I am trying to use it using the first version of the code.

I currently have 3 tables in my database. My status table has all the statuses used in the web application. I have a news table. And I have a news status table. The reason I did this is because I don’t want all the statuses to be available for news articles, just a couple of them. So my 3 tables will look like this:

News table:

NewsId int required primary key Title varchar required Body varchar required NewsStatusId int required foreign key 

NewStatus table:

 NewsStatusId int required primary key StatusId int required foreign key 

Status table

 StatusId int required primary key Name varchar required 

When creating classes for this, do I need to create classes for News, Status and NewsStatus? I only thought about the news and status? What would my relationship between classes 2/3 look like?

My News class is as follows

 public class News { public int NewsId { get; set; } // rest of my properties public int StatusId { get; set; } } 

Status Class:

 public class Status { public int StatusId { get; set; } public string Name { get; set; } } 

What would these classes look like with the relationships between classes 2/3?

Any code samples will be appreciated.

+7
source share
2 answers

You really don't need a free API for the basic Many-to-Many relationship between News and Status. Everything will be deduced by Code First base on conventions. However, we can customize your database schema using a free API based on your requirements.

 public class News { public int NewsId { get; set; } public string Title { get; set; } public ICollection<Status> Statuses { get; set; } } public class Status { public int StatusId { get; set; } public string Name { get; set; } public ICollection<News> Newses { get; set; } } public class Ctp5Context : DbContext { public DbSet<News> Newses { get; set; } public DbSet<Status> Statuses { get; set; } } 

Note that in a Many to Many association like this, NewsStatusId will not be the foreign key in the News class, instead the NewsId foreign key will be displayed in the link table that links to the PC on the news page. If you really need to have a NewsStatusId in the News class, then we must break this association into 2 one-to-many associations, which means we have 3 objects, not 2.

+6
source

Just News and Status . You should write something like:

 modelBuilder.Entity<News>() .HasMany(n => n.Statuses) .WithMany(s => s.News); 

(Customize pluralization to your liking.)

+1
source

All Articles