c# - Entity Framework Code First Approach -
public class contextex:dbcontext { public void modelcreate(dbmodelbuilder modelbuilder) { database.setinitializer<contextex>(null); modelbuilder.entity<category>().totable("categories"); modelbuilder.entity<category>().haskey(c=> new {c.categoryid}); modelbuilder.entity<cartitem>().totable("cartitems"); modelbuilder.entity<cartitem>().haskey(ci=> new {ci.cartid}); modelbuilder.entity<product>().totable("products"); modelbuilder.entity<product>().haskey(p=> new {p.productid}); modelbuilder.entity<order>().totable("orders"); modelbuilder.entity<order>().haskey(en=> new { en.orderid}); modelbuilder.entity<orderdetail>().totable("orderdetails"); modelbuilder.entity<orderdetail>().haskey(od=> new {od.orderdetailid }); } public dbset<category> categoriesdb { get; set; } public dbset<product> products { get; set; } public dbset<order> orders { get; set; } public dbset<orderdetail> details { get; set; } public dbset<cartitem> items { get; set; } } /********************second class*****************************/ namespace wing.tip.toys.dal.model { public class second { public static void main(string[] args) { try { contextex cont = new contextex(); list<category> category = cont.categoriesdb.tolist<category>(); } catch (exception e) { console.writeline(e.message.tostring()); } } } }
here in line - list category = cont.categoriesdb.tolist(); throwing me following exception:
one or more validation errors detected during model generation:
\tsystem.data.entity.edm.edmentitytype: : entitytype 'cartitem' has no key defined. define key entitytype. \tsystem.data.entity.edm.edmentityset: entitytype: entityset 'items' based on type 'cartitem' has no keys defined.
i'm confused do. have searched everywhere didn't desired solution. please me.
one of possible problems code instead of
protected override void onmodelcreating( dbmodelbuilder modelbuilder )
you have
public void modelcreate(dbmodelbuilder modelbuilder)
thus metadata setup method never called.
Comments
Post a Comment