I have two model classes
public class Genre
{
[Key]
public int GenreId { get; set; }
public string GenreName { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
}
and
public class Movie
{
[Key]
public int MovieId { get; set; }
public string MovieName { get; set; }
public string MovieDirector { get; set; }
public int MovieYear { get; set; }
public string MovieDesc { get; set; }
public DateTime CreationDate { get; set; }
public int GenreRefId { get; set; }
[ForeignKey("GenreRefId")]
public virtual Genre Genre { get; set; }
}
I know how to display a list of these objects on their own but my project requires that I list:
Each genre + the top 5 newest movies added to the list
I'm not even sure where to begin. Since you cannot use two models in a view... how do i gather and display both lists?
any help is much appreciated!
Related
In my store I have an orders table that captures the orders made by each customer. I want to be able to show the user what quantity of an item they have in their cart. Here is my orders model:
public class Order
{
public int Id { get; set; }
public ApplicationUser Customer { get; set; }
public string ShirtCause {get; set;}
public DateTime CreateDate { get; set; }
public ShirtSize ShirtSize { get; set; }
public ShirtQuantity ShirtQuantity { get; set; }
public decimal Total { get; set; }
public string ShirtYear { get; set; }
public int OrderNum { get; set; }
public bool? OrderCompleted { get; set; }
public bool? RecievedShirt { get; set; }
}
Using code first ShirtQuantity is created in a table and is reflected in the column ShirtQuantity_Id. For reference purposes, here is my quantity model:
public class ShirtQuantity
{
public int Id { get; set; }
public int Name { get; set; }
}
The cart should be output and enumerated in the action with:
return View(db.Orders.ToList());
All except for the Quantity_Id will list when calling
#model IEnumerable<ProjectName.Models.Order>
in the view. How do I get these quantities to list? I have tried creating a view model combining orders and shirtQuantities and calling it in the view:
public class ShirtOrdersViewModel
{
public int ShirtSizeId { get; set; }
public IEnumerable<ShirtSize> ShirtSizes { get; set; }
public int ShirtQuantityId { get; set; }
public IEnumerable<ShirtQuantity> ShirtQuantities { get; set; }
public int ShirtId { get; set; }
public IEnumerable<Shirt> Shirts { get; set; }
public int FileId { get; set; }
public IEnumerable<File> Files { get; set; }
public int OrderId { get; set; }
public IEnumerable<Order> Orders { get; set; }
public decimal Total { get; set; }
}
and this also does not work.
I have Country, City, Region and "Account Address" tables.
I want to create foreign key columns in "Account Address" pointing to Country, City, Region tables.
I have this code but it throws an error on creating database
The property \u0027Account_Id\u0027 cannot be configured as a
navigation property. The property must be a valid entity type and the
property should have a non-abstract getter and setter. For collection
properties the type must implement
After New Edit
public class Cities
{
[Key]
public int City_Id { get; set; }
public string City_name { get; set; }
public int Country_Id { get; set; }
[ForeignKey("Country_Id")]
public Countries countries { get; set; }
}
public class Region
{
[Key]
public int Region_Id { get; set; }
public string Region_name { get; set; }
public int City_Id { get; set; }
[ForeignKey("City_Id")]
public Countries countries { get; set; }
}
public class Accounts
{
[Key]
public int Account_Id { get; set; }
public string Fullname { get; set; }
public string Email { get; set; }
public string password { get; set; }
public int Cell_phone { get; set; }
public string Role { get; set; }
public int? estate_office_Id { get; set; }
[ForeignKey("estate_office_Id")]
public Estate_office estate_office { get; set; }
public List<Ads> ads { get; set; }
}
public class Account_address
{
[Key]
public int Id { get; set; }
[ForeignKey("Account_Id"), Column(Order = 0)]
public int Account_Id { get; set; }
[ForeignKey("Country_Id"), Column(Order = 1)]
public int Country_Id { get; set; }
[ForeignKey("City_Id"), Column(Order = 2)]
public int City_Id { get; set; }
[ForeignKey("Region_Id"), Column(Order = 3)]
public int Region_Id { get; set; }
public Accounts accounts { get; set; }
public Countries countries { get; set; }
public Cities cities { get; set; }
public Region region { get; set; }
}
You need to define public properties as shown below on the Account_address class.Then only EF will know how to map those navigation properties correctly.
public class Account_address
{
......
......
public Accounts accounts { get; set; } //like this
public Countries countries { get; set; } //like this
public Cities cities { get; set; } //like this
public Region region { get; set; } //like this
}
Update :
Hence you're not using singular naming convention for the classes,you have encountered this issue.Either you have to change the name of classes as singular or need to change the navigational property names a shown below.You have to do this for all the places.Here I have shown only for the Accounts class related navigational property.
[ForeignKey("Accounts_Id"), Column(Order = 0)]
public int Accounts_Id { get; set; }
My Advice is to follow the basic naming conventions.Then you can avoid lot of above kind of weird errors.
I have two models:
public class Project
{
public int ProjectID { get; set; }
public string Name { get; set; }
public string ShortDesc { get; set; }
public string LongDesc { get; set; }
public int Status { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Image { get; set; }
public List<Note> Notes { get; }
}
and:
public class Note
{
public int NoteID { get; set; }
public int ProjectID { get; set; }
public string Name { get; set; }
public string Content { get; set; }
public int Status { get; set; }
public DateTime Timestamp { get; set; }
public string Tags { get; set; }
}
My goal is to be able to easily relate Note and Project objects in view.
To store ProjectID in Note object I do:
// GET: Notes/Create
public ActionResult Create() {
ViewBag.Projects = db.Projects.ToList();
return View();
}
And later in Create view:
#Html.DropDownListFor(model => model.ProjectID, new SelectList(ViewBag.Projects, "ProjectID", "Name"))
Which allows me to pick related project by it's name.
How to correctly swap ProjectID with Project.Name on Index page (where all my notes are shown)?
Also I would like to see related Notes column in Project Index (where all my Projects are listed). Is it possible with use of Notes List from ProjectModel?
You can add a Project property to your Note class.
public class Note
{
public int NoteID { get; set; }
public int ProjectID { get; set; }
public string Name { get; set; }
public string Content { get; set; }
public int Status { get; set; }
public DateTime Timestamp { get; set; }
public string Tags { get; set; }
public virtual Project Project {set;get;}
}
And now you can access this project property in your view
#model List<Note>
#foreach(var item in Model)
{
<p>#item.Name</p>
<p>#item.Project.Name</p>
}
Hi every one I want to save complex data using Entity Framework and C#. I have 2 classes Product and Order defined as follows
Product Class
public class Product
{
[Key]
public int Id { get; set; }
public string SKU_Code { get; set; }
public string Product_Name { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
public string Image { get; set; }
public DateTime Created_Date { get; set; }
public DateTime Modified_Date { get; set; }
}
Order Class
public class Order
{
[Key]
public long ID { get; set; }
public string Order_Id { get; set; }
public string Payment_Type { get; set; }
public string Customer_Name { get; set; }
public string Shipping_Address { get; set; }
public DateTime Order_Date { get; set; }
public DateTime Modified_Date { get; set; }
public bool Flag { get; set; }
public List<Product> ProductDetails { get; set; }
}
And I want to save data Order details and my piece of code is as follows.
public Order Add(Order odrerDetails)
{
using (var context = new EcommerceDBContext())
{
var MyOrder_Id = Helper.Random(7); //Generate random orderID from my class
foreach (var detail in odrerDetails.ProductDetails)
{
odrerDetails.Order_Id = MyOrder_Id;
odrerDetails.Quantity = Convert.ToInt32(detail.Quantity);
odrerDetails.Amount = Convert.ToDouble(detail.Price);
//Other Details
context.objOrderListing.Add(odrerDetails);
}
context.SaveChanges();
return odrerDetails;
}
}
This gives me perfect data but when it comes to context.SaveChanges(); it return's me error.
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types.
To me you domain model seems all wrong. The order should just be used for grouping, its a typical e-commerce scenario.
When you get a receipt of your purchases, you get one receipt with every Item and price listed next to it. Its considered as one order of multiple things, not multiple orders of multiple things.
Reading your last comment, you cant have multiple orders with the same order id. Try to understand the domain first before trying to solve it with code. Also,you have no notion of a Customer with an Order.
public class Product
{
[Key]
public int Id { get; set; }
public string SKU_Code { get; set; }
public string Product_Name { get; set; }
public string Price { get; set; }
public string Image { get; set; }
public DateTime Created_Date { get; set; }
public DateTime Modified_Date { get; set; }
}
public class Order
{
[Key]
public long ID { get; set; }
public string Order_Id { get; set; }
public string Payment_Type { get; set; }
public string Customer_Name { get; set; }
public string Shipping_Address { get; set; }
public DateTime Order_Date { get; set; }
public DateTime Modified_Date { get; set; }
public bool Flag { get; set; }
public List<OrderLineItem> Items { get; set; }
}
public class OrderLineItem
{
[Key]
public long ID { get; set; }
public long Order_Id { get; set; }
public long Product_Id {get; set;}
public int Quantity {get; set;}
}
I work with EF for the first time so I don't know is situation like this normal or I have serious performance issues.
I have following situation:
Bellow are the classes that I have. Item is the main object here. So when I pull a list of Items from database I get for example 1000 items. And now each of this item has all properties filed with data. City contains Country, Country contains list of cities, User has list of created items, each item all data again, city, city has country, country list of cities etc etc...
Maybe I am worrying too much and I don't know should this object's contain all those data and does this make performance issues, or I am doing something wrong here?
public abstract class Item
{
[Key]
public int ItemId { get; set; }
public int ItemTypeId { get; set; }
public Guid UserId { get; set; }
public DateTime CreatedOnDate { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int? MediaId { get; set; }
public int CityId { get; set; }
public virtual City City { get; set; }
public virtual User User { get; set; }
public virtual ICollection<ItemInBoard> ItemsInBoard { get; set; }
public virtual ICollection<Like> Likes { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class City
{
public int CityId { get; set; }
public string Name { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
public int CountryId { get; set; }
public virtual Country Country { get; set; }
}
public class Country
{
public int CountryId { get; set; }
public string Name { get; set; }
public string CountryCode { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
public class User
{
public Guid UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Gender { get; set; }
public DateTime? BirthDay { get; set; }
public string AboutMe { get; set; }
public int? MediaId { get; set; }
public int CityId { get; set; }
public virtual City City { get; set; }
public virtual ICollection<Item> Items { get; set; }
public virtual ICollection<Board> Boards { get; set; }
public virtual ICollection<Like> Likes { get; set; }
}
It is up to you. This is a concept called lazy loading. You can enable or disable lazy loading with this code:
context.Configuration.LazyLoadingEnabled = false;
context.Configuration.LazyLoadingEnabled = true;
When enabling this option none of the dependent entities will be loaded. To enforce dependent entities to load you can use the Include lambada expression like this:
var test = context.Tests.Include("SomeOtherDependentEntity");
Hope I got you and this is what you meant.
I would say that what you have is fine for general business logic.
When I have to do a lot of time sensitive processing in a read-only fashion I use SQL commands like this to get exactly and only exactly what I want.
public class myQueryClass
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
var context = new MyDbContext();
context.Database.SqlQuery<myQueryClass>("SELECT Property1 = acolumn, Property2 = acolumn2 FROM myTable WHERE something = somestate");