Entity Framework Contains method and objects - entity-framework

Is there a way of using the Contains method in Entity Framework 4 with the actual id of the object?
Take these entities as an example:
public class Order
{
public int OrderId { get; set; } // PK
public string CustomerId { get; set; } // FK to Customer
}
public class OrderItem
{
public int OrderId { get; set; } // PK
public int ItemId { get; set; } // PK, FK to Item
}
public class Item
{
public int ItemId { get; set; } // PK
public string ItemName { get; set; }
}
and I want to return a list of all orders that have item '5' in them.
I want to be able to do:
List<Order> orders = db.Orders.Where(m => m.OrderItems.Contains(5)).ToList()
But that won't work because Contains needs an actual OrderItem object.
Thanks

var orders = db.Orders.Where(m => m.OrderItems.Any(i => i.OrderId == 5)).ToList();

Try this:
List<Order> orders = db.Orders.Where(m => m.OrderItems.Any(i=>i.OrderId==5)).ToList()
OR
List<Order> orders = db.OrderItems.Where(m => m.OrderId==5).Orders.ToList()

Related

EF Core - Cannot add object with composite key

Alright, I'm trying to make a Wishlist containing user favorite items, however when I try to add them to the user, EF doesn't even try to INSERT, no action.
Here is my FavoriteProduct model
public class FavoriteProduct : BaseDeletableModel<int>
{
public string FashionNovaUserId { get; set; }
public FashionNovaUser FashionNovaUser { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
builder.Entity<FavoriteProduct>().HasKey(x => new { x.ProductId, x.FashionNovaUserId });
Here's my user model
public class FashionNovaUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<FavoriteProduct> FavoriteProducts { get; set; }
Then through my service layer I'm trying to add the favoriteProduct to user's list
var favoriteProduct = new FavoriteProduct
{
ProductId = id,
FashionNovaUserId = user.Id
};
user.FavoriteProducts.Add(favoriteProduct);
this.db.SaveChanges();
When I do that, there database table is not updated, nor it has any new entries.
Since FashionNovaUser and Product are many-to-many relationships, if you would like to add records of FavoriteProduct in join table, just use
var favoriteProduct = new FavoriteProduct
{
ProductId = id,
FashionNovaUserId = user.Id
};
this.db.Add(favoriteProduct);//or this.db.FavoriteProduct.Add(favoriteProduct)
this.db.SaveChanges();

Merge Key constraint with Entityframework

I am a .NEt student trying to build a simple pizza webbshop. I am using Entityframework to build the database. The errormessage I get is this:
The MERGE statement conflicted with the FOREIGN KEY constraint "FK_Products_Categories_CategoryID". The conflict occurred in database "TomasosPizzeria", table "dbo.Categories", column 'CategoryID'.
The statement has been terminated.
I have googled and tried to solve this pne alone by renaming the properties back and forth. Here are my classes:
public class Product
{
public int ID { get; set; }
public string ProductName { get; set; }
public int ProductPrice { get; set; }
public string Ingridients { get; set; }
//--
public List<OrderDetail> OrderDetails { get; set; }
[ForeignKey("Categories")]
public int CategoryID { get; set; }
public virtual Category Categories { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
List<Product> Products { get; set; }
}
I am using a DbInitializer to set som test values. I have had problems giving the product object a category with the corresponding value.
var categories = new Category[]
{
new Category{CategoryName="Pizza"},
new Category{CategoryName="Pasta"},
new Category{CategoryName="Sallad"},
};
var products = new Product[]
{
new Product{ProductName="Äcklig Calzone", Ingridients="Gamla tomater, möglig ost, trötta oliver, Unken skinka", ProductPrice=150},
new Product{ProductName="Italiensk skit", Ingridients="Proscutti, halvfärdig mozzarella, trötta oliver, skämd vattnig proscutti", ProductPrice=250},
new Product{ProductName="La bussola", Ingridients="Äckliga tomater, giftiga räkor, levande musslor, rutten skinka",CategoryID = 2,<--Here-- ProductPrice = 105 }
};
foreach (Product s in products)
{
context.Products.Add(s);
}
context.SaveChanges();
I am thankful for all the help I get. Please tell me if you wish to see any more of my code.
Solved it by giving CategoryID to the two other products.

Inserting data into multiple tables using Entity Framework

I have two tables in my database that I am filling via Web API:
Orders
________
OrderID
OrderDate
OrderStatusID
and
OrderItems
___________
OrderID
ItemID
ItemVersionID
ItemNote
ItemSortOrder
I need to insert Order and all the items for that order in two tables. OrderID is an identity field generated by the database that I will need for inserting data into OrderItems table.
Primary key for the OrderItems table is a composite key (OrderID, ItemID, ItemVersionID), it is important since the same order can contain multiple items with the same ID but different Version ID.
I was wondering if I will have to add Order and OrderItems data separately or can do so in a single controller function.
Below are my model classes:
[Table("SN_Orders")]
public class Order
{
[Key]
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
public int OrderStatusID { get; set; }
public List<OrderItem> Details { get; set; }
}
[Table("SN_OrderItems")]
public class OrderItem
{
[Column(Order = 0), Key]
public int ItemID { get; set; }
[Column(Order = 1), Key]
public int ItemVersionID { get; set; }
[Column(Order = 2), Key]
public int OrderID { get; set; }
public string ItemNote { get; set; }
public int ItemSortOrder { get; set; }
}
And below is my attempt to pass in a list of OrderItems with Order that did not work out:
[HttpPost]
public IHttpActionResult PostItemToOrder(myClass.Order ord1, List<myClass.OrderItem> itemList)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
using (MyContext ctx = new MyContext())
{
ctx.Orders.Add(ord1);
ctx.SaveChanges();
foreach (var item in itemList)
{
item.OrderID=ord1.OrderId;
ctx.OrderItems.Add(item);
ctx.SaveChanges();
}
}
}
Is that something that is possible to accomplish at once? Or will I have to insert order first, return the OrderID to the calling program and then insert Items?
Put it like this:
Your Order class should have:
public class Order
{
public int Id { get; set; }
// all rest
public virtual ICollection<OrderItem> Items { get; set; } // here is the trick
}
[HttpPost]
public IHttpActionResult PostItemToOrder(myClass.Order ord1, List<myClass.OrderItem> itemList)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
using (MyContext ctx = new MyContext())
{
var order = ord1;
order.Items = itemList;
ctx.Orders.Add(order);
ctx.SaveChanges();
}
}

Storing data in two tables at a time in Entity Framework

I have three tables
Products { pid, pname, price }
Orders { oid, odate }
OrderDetails { odid, oid, pid, qty, total }
I've a form which upon submission passes an array of OrderDetails which I want to store in database. But the tragedy here is I want to store current date in Order table at the same time. Now my Order table is getting populated with odate but nothing is getting inserted in my orderdetails table. And I'm using web api and the array is fetched correctly in controller. I guess this line
aOrder.AllOrders.Add(od);
in the controller has to be replaced with somethting else.
Order.cs
public class Order
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int oid { get; set; }
[DataType(DataType.DateTime)]
public DateTime odate { get; set; }
public virtual List<OrderDetail> AllOrders { get; set; }
public Order()
{
AllOrders = new List<OrderDetail>();
}
}
OrderDetail.cs
public class OrderDetail
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int odid { get; set; }
public int oid { get; set; }
public virtual Order order { get; set; }
public int pid { get; set; }
public int qty { get; set; }
public int total { get; set; }
public virtual Product Aproduct { get; set; }
public OrderDetail()
{
Aproduct = new Product();
}
}
OrderDetailsController.cs
private static readonly IOrderDetailRepository _orders = new OrderDetailRepository();
public Order Post(List<OrderDetail> orderDetails)
{
Order aOrder = new Order();
foreach(OrderDetail orderDetail in orderDetails)
{
OrderDetail od = new OrderDetail();
od.oid = orderDetail.oid;
od.pid = orderDetail.pid;
od.qty = orderDetail.qty;
od.total = orderDetail.total;
aOrder.AllOrders.Add(od);
}
aOrder.odate = DateTime.Now;
return _orders.Add(aOrder);
}
OrderDetailRepository.cs
public Order Add(Order order)
{
_db.Orders.Add(order);
_db.SaveChanges();
return order;
}
Can you try specifying a foreign key for OrderDetails:
public class OrderDetail
{
[ForeignKey("Order")]
public int oid { get; set; } //I'm guessing this is Order's PK

EF4 Mapping one-to-many on existing database without navigation

I'm using ModelBuilder to map an existing database to POCOs. I have courses, students, and meetings. Here are the tables
CREATE TABLE Courses (
CourseID int,
Name string)
CREATE TABLE Students(
StudentID int,
Name string)
CREATE TABLE Courses_Students (
CourseID int,
StudentID int)
CREATE TABLE Meetings (
MeetingID int,
CourseID int,
MeetDate datetime)
And the POCOs
public class Course {
public int CourseID { get; set; }
public string Name { get; set; }
public virtual ICollection<CourseMeeting> Meetings { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Student {
public int StudentID { get; set; }
public string Name { get; set; }
}
public class Meeting {
public int MeetingID { get; set; }
public int CourseID { get; set; }
public DateTime MeetDate { get; set; }
}
The table mapping works great:
modelBuilder.Entity<Course>().MapSingleType().ToTable("Courses");
modelBuilder.Entity<Student>().MapSingleType().ToTable("Students");
modelBuilder.Entity<Meeting>().MapSingleType().ToTable("Meetings");
And the many-to-many mapping with a join table and without a navigation property works (i.e. there is no Students.Courses property specified on WithMany())
modelBuilder.Entity<Course>()
.HasMany(c => c.Students)
.WithMany()
.Map(StoreTableName.FromString("Courses_Students"),
(c, s) => new { CourseID = c.CourseID, StudentID = s.StudentID});
But I'm having trouble mapping the other relationship that doesn't have a join table. This obviously isn't right:
modelBuilder.Entity<Course>().HasMany(c => c.Meetings).WithMany();
Because it wants a join table: Invalid object name 'dbo.Course_Meetings'. I can add a Course property to the Meeting object and then use
modelBuilder.Entity<Course>()
.HasMany(c => c.Meetings)
.WithOptional(m => m.Course)
.HasConstraint((c, m) => c.CoursID == me.CourseID);
But I'd like to do this without the navigation property. Is this possible with EF4 and an existing database?
It's assuming it needs the join table (and thus looking for it) because you haven't mapped the property in the original declaration.
Try manually mapping the properties on the actual table like this..
public class Meeting {
public int MeetingID { get; set; }
public int CourseID { get; set; }
public DateTime MeetDate { get; set; }
public Course { get; set; }
}
and then configure it as follows:
modelBuilder.Entity<Meeting>(m => new {
MeetingId = m.Meeting,
MeetDate = m.MeetDate,
CourseId = m.Course.Id
})
.HasRequired(m => m.Course)
.WithMany()