sql server - .NET and Entity Framework Database creation -
i want create sql server database in .net using entity framework, can't find how it.
as of have:
- a simple project (wcf service) default name
a database model
a sql file should create database; generated using model (generate database model)
a service file (
.svc
)
code:
public class service1 : iservice1 { public entity1 getdata(int id) { using (model1container ctx = new model1container()) { try { entity1 entity = ctx.entity1set.firstordefault(e => e.id == id); return entity; } catch (exception e) { console.writeline("captain, got error: " + e); return null; } } } public string postdata(string value) { using (model1container ctx = new model1container()){ entity1 newentity = new entity1(); newentity.property1 = value; ctx.entity1set.add(newentity); try { ctx.savechanges(); } catch (exception e) { return ("captain, got error: " + e); } } return "0"; } public compositetype getdatausingdatacontract(compositetype composite) { if (composite == null) { throw new argumentnullexception("composite"); } if (composite.boolvalue) { composite.stringvalue += "suffix"; } return composite; } }
but problem is seems database isn't created. cannot find how run sql file, server explorer shows nothing, , when try request in database got following error:
system.data.sqlclient.sqlexception: invalid object name 'dbo.entity1set'.
did forgot important?
if using sql server ce, can tell context if database file not exists , create file:
database.setinitializer( new createdatabaseifnotexists<yourcontext>() );
if using sqlite, context right place create database functionality not supported in entity framework , have things 'manually'. see here.
if dealing sql server or other 'real db' procedure similar this
Comments
Post a Comment