sql server - Mapping MVC3 model to MSSQL 2008 View using code first and database first -
in sql server 2008 have view this:
create view products select a.id, a.name, b.id subid, b.name subname main_product inner join sub_product b on a.id = b.mainid this model:
public class products { public int id { get; set; } public int name { get; set; } public int subid { get; set; } public int subname { get; set; } } now how can map mvc3's view sql server view?
update
i tried this:
public class partialcontext : dbcontext { protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<pluralizingtablenameconvention>(); } public dbset<test> test { get; set; } public dbset<products> products { get; set; } } and controller:
public class viewcontroller : controller { private partialcontext db = new partialcontext(); // // get: /view/ public viewresult index() { return view(db.products.tolist()); } } this effort gave me error: "invalid object name 'dbo.products'."
edit 2
just clarify. want use both code first , database first technique. means want create both database , model class manually, not through code generator or database generator
update
problem solve. make mistake on naming model class
update ef model

now can query view
using (var ctx = new yourdbcontext()) { var result = t in ctx.product select new products{ id= ctx.id, name=ctx.name }; } more information on following link
http://www.mssqltips.com/sqlservertip/1990/how-to-use-sql-server-views-with-the-entity-framework/
i think you.
Comments
Post a Comment