Entity Framework, relation between two tables - asp.net-mvc

My model classes are:
public class User
{
public User()
{
Polls = new List<Poll>();
}
public int Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
ICollection<Poll> Polls { get; set; }
}
public class Poll
{
public int Id { get; set; }
public int PositiveCount { get; set; }
public int NegativeCount { get; set; }
public String Description { get; set; }
public User User { get; set; }
}
public class PollsContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Poll> Polls { get; set; }
}
EF created a table User_Id column for Polls table.
In the Create view of the Poll, I want to specify the UserId for new poll belongs too, but intellisense shows there is no access to model.UserId, there is a model.User.Id, which is not allowing me create a Poll for existing user and rather creates new User with new Id and no association is created.
<div class="editor-label">
#Html.LabelFor(model => model.User.Id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.User.Id)
#Html.ValidationMessageFor(model => model.User.Id)
</div>
What is the right way of creating a new Poll for an existing User then ?

public class Poll
{
public int Id { get; set; }
public int PositiveCount { get; set; }
public int NegativeCount { get; set; }
public String Description { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}

I think your Poll class should look like:
public class Poll
{
public int Id { get; set; }
public int PositiveCount { get; set; }
public int NegativeCount { get; set; }
public String Description { get; set; }
public int User_Id { get; set; }
public User User { get; set; }
}
If you have a column in your Poll table User_Id, then your model should also have this column. You can then just assign a User_Id to a Poll and Entity Framework will take care of the correct linking to the right User object.
Further, consider making your association properties (Poll.User and User.Polls) virtual in order to use the lazy loading mechanisms of Entity Framework.
See this

Related

How do I map data from a DTO to a navigational property?

I have two models, User and Review, which have a one to many relationship. I am using a DTO to create new reviews in my controller. Reviews are added to an ICollection of type Review on a Movie model, (movies and reviews also have a one to many relationship). Here's a snippet from my controller:
var query = db.Movies.Where(i => i.id == reviewDTO.movieId).FirstOrDefault();
Review review = new Review()
{
goreRating = reviewDTO.goreRating,
shockRating = reviewDTO.shockRating,
jumpRating = reviewDTO.jumpRating,
plotRating = reviewDTO.plotRating,
supernaturalRating = reviewDTO.supernaturalRating,
description = reviewDTO.description,
isSpoiler = reviewDTO.isSpoiler
};
try
{
query.Reviews.Add(review);
}
And the models:
public class User
{
public int id { get; set; }
public string Username { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
}
public class Review
{
public int id { get; set; }
public virtual Movie Movie { get; set; }
public virtual User User { get; set; }
public int goreRating { get; set; }
public int shockRating { get; set; }
public int jumpRating { get; set; }
public int plotRating { get; set; }
public int supernaturalRating { get; set; }
public string description { get; set; }
public bool isSpoiler { get; set; }
}
How can I add the relationship in the controller using entity framework and set the foreign key userId for each review?
To add a relation between User and Review you are just missing the UserId in Review
public class Review
{
....
public int UserId { get; set; } // Add this to review model
....
}
To get users with all their reviews you can write use this kind of LINQ query
var data = _context.User.Where(x => ...any condition here...).Include(x => x.Reviews )

Need help trying to create a one-to-many relationship using EF7, Asp.NET and SQLite

I am new to Entity Framework and Asp.NET, and therefore, struggling with creating database relationships within the Entity Framework.
I have two SQLite tables (Ticket and User) and have setup my entity models as follows:
public class Users
{
[ForeignKey("id")]
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public virtual ICollection<Tickets> Tickets { get; set; }
}
public class Tickets
{
public int id { get; set; }
public string summary { get; set; }
public string description { get; set; }
public string c_location { get; set; }
public string c_store_device { get; set; }
public string category { get; set; }
public DateTime? created_at { get; set; }
public DateTime? closed_at { get; set; }
public int priority { get; set; }
public int? assigned_to { get; set; }
public DateTime? due_at { get; set; }
public DateTime? updated_at { get; set; }
public string status { get; set; }
public virtual Users Users { get; set; }
}
I am trying to use Entity Framework 7 to export an IEnumerable<Tickets> that includes the User assigned to each Ticket.
I have tried to create my model relationship in MyDBContext as a single User can have multiple Tickets, and also has a foreign key associated in my Sqlite database (Tickets.assigned_to = User.id):
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Users - > many Tickets
modelBuilder.Entity<Users>()
.HasMany(p => p.Tickets)
.WithOne(e => e.Users)
.HasForeignKey(p => p.assigned_to);
}
My result ends up with Ticket data being exported, but against every ticket I see a null value for User:
[{"id":10002,...,"Users":null}]
When I use .Include() within my Repository to include each User like this:
public IEnumerable<Tickets> GetAll()
{
return _db.Tickets.Include(t => t.Users).ToList();
}
It results in the error
HTTP Error 502.3 - Bad Gateway
The specified CGI application encountered an error and the server terminated the process.
What I'm trying to retrieve is data that looks like:
{"Ticket";[{"id":10002,..."status":"closed"}],"Users":[{"id":"1"..."email":"johndoe#someplace.com"}]}
I know it probably has something to do with my relationship model, but I cannot work out what I am doing wrong.
First you should really derive your Users from IdentityUser. It helps when trying to wire up the relationship, but I will give you the answer based on your current models. Your ForeignKey property should be on the child entity. By naming conventions, which is what EF uses by default, your public Users Users works better if you put a public int UsersId. Then essentially what EF will do is from your public Users Users it will go to the Users table. Then it looks for the ForeignKey which is set to Id, so now we are in the Users Table looking at the id property. Then it looks for the naming convention UsersId and if it sees it, it will set that property to the value that it saw from the Users Table Id column.
Try using this
public class Users
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public virtual ICollection<Tickets> Tickets { get; set; }
}
public class Tickets
{
public int id { get; set; }
public string summary { get; set; }
public string description { get; set; }
public string c_location { get; set; }
public string c_store_device { get; set; }
public string category { get; set; }
public DateTime? created_at { get; set; }
public DateTime? closed_at { get; set; }
public int priority { get; set; }
public DateTime? due_at { get; set; }
public DateTime? updated_at { get; set; }
public string status { get; set; }
[ForeignKey("Id")]
public int UsersId { get; set; }
public virtual Users Users { get; set; }
}
and for your Fluent API configuring
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Users - > many Tickets
modelBuilder.Entity<Users>()
.HasMany(p => p.Tickets)
.WithOne();
}
Now all that does is create the relationship. In order to view the specific items you want to view, use a ViewModel. So, pull the two lists you want from where you want. Then use logic to separate the list how you want them to display.
public class UsersViewModel()
{
public UsersViewModel(Users user, List<Tickets> tickets)
{
this.first_name = user.first_name;
this.last_name = user.last_name;
this.email = user.email;
this.Tickets = new List<Tickets>();
foreach(var ticket in tickets)
{
if(ticket.UserId == user.Id)
{
this.Tickets.Add(ticket)
}
}
}
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public List<Tickets> Tickets { get; set;}
}
then in your controller make your list
public IActionResult Index()
{
var usersList = _repository.Users.ToList();
var ticketsList = _repository.Tickets.ToList();
var model = new List<UsersViewModel>();
foreach(var user in usersList)
{
var listItem = new UsersViewModel(user, ticketsList);
model.Add(listItem);
}
return View(model);
}
or use a Linq query
public IActionResult Index()
{
var usersList = _repository.Users.ToList();
var model = new List<UsersViewModel>();
foreach(var user in usersList)
{
var ticketsList = from x in _repository.Tickets where x.UserId.Equals(user.Id) select x;
var listItem = new UsersViewModel(user, ticketsList);
model.Add(listItem);
}
return View(model);
}
then at the top of your view you should have
#model IEnumerable<UsersViewModel>

How to use dynamically Radiobutton in ASP.NET MVC

I am creating a Poll in my ASP.NET MVC project.
1) I select poll and it's replies as an object of Poll model from database [poll model contain a list of reply model that named PollReplies]:
Poll model:
public class Poll
{
public Poll() {
PollReplies = new List<PollReply>();
}
public int ID { get; set; }
public System.DateTime CDate { get; set; }
public System.DateTime SDate { get; set; }
public System.DateTime EDate { get; set; }
public string User_ID { get; set; }
public string Title { get; set; }
public int Language_ID { get; set; }
public bool IsActive { get; set; }
public bool IsPublic { get; set; }
public bool ResultIsPublic { get; set; }
public List<PollReply> PollReplies { get; set; }
}
Reply model:
public class PollReply
{
public int ID { get; set; }
public int Poll_ID { get; set; }
public string Title { get; set; }
public int Count { get; set; }
public bool IsChecked { get; set; }
}
2) I render poll title of the selected poll object as question of poll.
3) With a for render replies:
for (int i = 0; i < Model.PollReplies.Count; i++)
{
#Html.HiddenFor(model => model.PollReplies[i].ID, new { #Value = Model.PollReplies[i].ID })
counter++;
<tr>
<td>#counter</td>
<td>#Html.DisplayFor(model=>model.PollReplies[i].Title)</td>
<td>
#Html.RadioButtonFor(model => model.PollReplies[i].IsChecked,counter)
</td>
</tr>
}
My problem:
1) When I submit form, The Poll model is contain all replies. All replies has ID but the IsChecked propery of replies not set with the client selected radiobutton!(all IsChecked of replies are false) How I can post the selected reply as true?
2) I am using #Html.RadioButtonFor and radios give name attributes automaticly, so the name attribute of generated inputs are diffrente, but i need to just select one of radios! What is the best solution?

Join two Models to display in webpage

I'm building an application using ASP.NET MVC4 with code first data migrations. I have an estimates model, a clients model, a DbContext, and a view model I created. I am wanting to display the company name in a drop down, with the company name tied to an estimate. I have a ClientId in both models. I also created a DbSet<> and that didn't work either when querying against it.
I tried to create a viewmodel that I thought I could simply query against and display through my controller. I'm not having any luck in getting this to work. After a day plus of looking on here and other places, I'm out of ideas.
How can I query/join the two models, or query the viewmodel to get the company name associated with the clientId? Thanks for the help.
Models:
public class Estimates
{
[Key]
public int EstimateId { get; set; }
public int ClientId { get; set; }
public decimal EstimateAmount { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime EstimateDate { get; set; }
public string EstimateNotes { get; set; }
}
public class Clients
{
[Key]
public int ClientId { get; set; }
public string CompanyName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public ICollection<Estimates> Estimates { get; set; }
public ICollection<Contracts> Contracts { get; set; }
}
public class ClientEstimateViewModel
{
public Clients Clients { get; set; }
public Estimates Estimates { get; set; }
}
public class NovaDb : DbContext
{
public NovaDb(): base("DefaultConnection")
{
}
public DbSet<Clients> Clients { get; set; }
public DbSet<Estimates> Estimates { get; set; }
public DbSet<Contracts> Contracts { get; set; }
public DbSet<Invoices> Invoices { get; set; }
public DbSet<ClientEstimateViewModel> ClientViewModels { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Controller:
NovaDb _db = new NovaDb();
ClientEstimateViewModel ce = new ClientEstimateViewModel();
public ActionResult Index()
{
var model =
(from r in ce.Clients
join x in ce.Estimates
where
//var model =
// from r in _db.Clients
// orderby r.CompanyName ascending
// select r;
return View(model);
}
Because you've created the relationship between client & estimate in your models, you should be able to create a query like this:
var query = from c in _db.clients
select new ClientEstimateViewModel
{
Clients = c,
Estimates = c.Estimates
}
Although you'd have to change your model so Estimates was public List<Estimates> Estimates { get; set; }
This would give you a collection of ClientEstimateViewModel which you could then pass to your view

Linq doesn't want to cooperate - DbExpressionBinding error

I am trying to get something from my database, i need it to be like that:
var tribunalCase = context.TribunalCases.Where(c => c.Voters.Any(v => v.Voter.UserName == User.Identity.Name))
.Select(c => c)
.ToList();
But then, it crashes when i try to use .Any() or .All(). I get the following error:
DbExpressionBinding requires an input expression with a collection
ResultType.
Parameter name: input
This is my model:
public class Tribunal
{
public int Id { get; set; }
public Account User { get; set; }
public DateTime Expires { get; set; }
public Thread Thread { get; set; }
public int Points { get; set; }
public String Comment { get; set; }
public int VotersCount { get; set; }
public List<Voters> Voters { get; set; }
}
public class Voters
{
public int Id { get; set; }
public Account Voter { get; set; }
public bool Vote { get; set; }
public Tribunal Tribunal { get; set; }
}
Which i configured like that:
modelBuilder.Entity<Tribunal>()
.HasOptional(t => t.Voters)
.WithRequired();
How can i fix this error?
Configuration is incorrect: Voters is a collection, so you should call HasMany, not HasOptional.

Resources