c# - how to deal with 2 entangled tables of a database? -
i'm trying learn asp.net web forms following "wingtip toys" tutorial made microsoft. in project, create database has 2 tables: "products" & "category". each product has category:
namespace wingtiptoys.models { public class product { [scaffoldcolumn(false)] public int productid { get; set; } [required, stringlength(100), display(name = "name")] public string productname { get; set; } . . . public int? categoryid { get; set; } public virtual category category { get; set; } } public class category { [scaffoldcolumn(false)] public int categoryid { get; set; } [required, stringlength(100), display(name = "name")] public string categoryname { get; set; } public virtual icollection<product> products { get; set; } } }
then create 2 tables:
public class productcontext : dbcontext { public productcontext() : base("wingtiptoys") { } public dbset<category> categories { get; set; } public dbset<product> products { get; set; } }
and there 2 methods: "getcategory" & "getproduct"
public class productdatabaseinitializer : dropcreatedatabaseifmodelchanges<productcontext> { protected override void seed(productcontext context) { getcategories().foreach(c => context.categories.add(c)); getproducts().foreach(p => context.products.add(p)); } private static list<category> getcategories() { var categories = new list<category> { new category { categoryid = 1, categoryname = "cars" }, . . . new category { categoryid = 5, categoryname = "rockets" }, }; return categories; } private static list<product> getproducts() { var products = new list<product> { new product { productid = 1, productname = "convertible car", description = "this convertible car fast! engine powered neutrino based battery (not included)." + "power , let go!", imagepath="carconvert.png", unitprice = 22.50, categoryid = 1 }, . . . new product { productid = 16, productname = "rocket", description = "this fun rocket travel height of 200 feet.", imagepath="rocket.png", unitprice = 122.95, categoryid = 5 } }; return products; } }
you can see code here @ microsoft page. use "listview" "selectmethod=getproduct" show products client category in "products list" page (here, middle of page), , clicking on product, show it's details in "product details" page (link above, end of age).
now have idea : want assign "imagepath" property each category show image background of "product details" page. example, if product "boat" category, want background boat.jpg, etc.
[display(name = "imagepath")] public string imagepath { get; set; }
as mentioned (and can see in microsoft page above), each product has category , each category has imagepath, tried code path (please see links above understand i'm saying! ):
<%#:item.category.imagepath%>
there no error before compiling code, when press f5 debug project one:
system.invalidoperationexception: there open datareader associated command must closed first.
how can deal it?
edit: exact question: how "imagepath" "category" table while using "getproduct" list view "selectmethod" dealing "products" table?
Comments
Post a Comment