Saturday, 15 February 2014

sql - Get model from m:n relationship -



sql - Get model from m:n relationship -

i've got problem on sql query model logic. here tables i'vev created project.

at first product table

public partial class products { public products() { this.orders_products = new hashset<orders_products>(); this.shoppingcarts_products = new hashset<shoppingcarts_products>(); } public int productid { get; set; } public string name { get; set; } public double cost { get; set; } public int ppb { get; set; } public int categoryid { get; set; } public virtual categories categories { get; set; } public virtual icollection<orders_products> orders_products { get; set; } public virtual icollection<shoppingcarts_products> shoppingcarts_products { get; set; } }

then shoppingcart table

public partial class shoppingcarts { public shoppingcarts() { this.shoppingcarts_products = new hashset<shoppingcarts_products>(); } public int shoppingcartid { get; set; } public string userid { get; set; } public system.datetime creationdate { get; set; } public virtual aspnetusers aspnetusers { get; set; } public virtual icollection<shoppingcarts_products> shoppingcarts_products { get; set; } }

and @ to the lowest degree connection table between

public partial class shoppingcarts_products { public int shoppingcartid { get; set; } public int productid { get; set; } public int count { get; set; } public virtual products products { get; set; } public virtual shoppingcarts shoppingcarts { get; set; } }

now need list of products in own shoppingcart. kind of model should create?

i lean k need write sql statement, includes both of maintables , merge them connection table. can't fnd samle @ internet. know sample or can give me hint, how solve problem?

after step need show view on layout site.

okay, seek write query linq sql format. seek show view partial view on layoutside.

@html.partial("_shoppingcartpartial", html.action("myshoppingcart","shoppingcarts")) // get: myshoppingcart public list<shoppingcarts_products> myshoppingcart() { list<shoppingcarts_products> myproducts = db.shoppingcarts_products.where(i => i.shoppingcartid == 1).tolist(); homecoming myproducts; }

thanks

you try:

select * shoppingcarts_products shoppingcarts_products.shoppingcartid = <variable> left bring together shoppingcarts on shoppingcarts.shoppingcartid = shoppingcarts_products.shoppingcartid left bring together products on shoppingcarts.productid = products.productid

also, expand field selection criteria select fields need.

sql asp.net-mvc ado.net

No comments:

Post a Comment