Reputation: 21
I am using:
I have now recreated my database in VS 2010 as a .fsi (not .msi) database.
OK, but can't work out how to do selects/inserts etc via the data model, rather than going through the hassle of creating a connection, parameters, etc etc. (That worked fine).
I have been trawling the web for days for answers, but it's always very detailed step-by-step guides from people using latest, most expensive versions.
I've set up data source and connections (to a DB built entirely in VS - don't even know if it's using a SQL engine that's still supported)
I've tried various ways of coding an INSERT which simply ends in SaveChanges()
, e.g.
using (TutorBrainsDBEntities2 tB = new TutorBrainsDBEntities2())
{
var cl = new Client();
cl.ClientFirstName "XXXXX";
cl.ClientLastName = "XXXXX";
tB.SaveChanges();
}
I also have using System.Data.EntityModel
in the Class, and a reference to System.Data.Entity
in the solution.
I know this isn't the best day to ask, but I'm under pressure here, and don't want to make-do with some over-complicated Excel file....so any help would be really appreciated (e.g. a link to an article covering this rather weird problem.)
Upvotes: 0
Views: 188
Reputation: 21
Thanks everyone for your comments. I was trying to use .Add() but it didn't come up as a valid option.
Nor can I manually enter rows into the tables by typing them in & using the Run SQL command. As It's stored the db file in both root and debug folders, someone said this is common, and to change the properties to "Never Copy" or something - anyway, I will work through it in 2016!! and hopefully something will work.
Best, D
Upvotes: 0
Reputation: 1349
Just make ClientID in database a identity column in SSMS and then try
using (TutorBrainsDBEntities2 tB = new TutorBrainsDBEntities2())
{
var cl = new Client();
cl.ClientFirstName = "XXXXX";
cl.ClientLastName = "XXXXX";
tB.Clients.Add(cl); //Add this line to add your newly created clients to the collection
tB.SaveChanges();
}
Without identity column i will fail to insert in DB
Upvotes: 0
Reputation: 77926
If you are using EF (mostly DB first approach) then your DB context class already do have a reference for Clients
entity collection and in such case do like
using (TutorBrainsDBEntities2 tB = new TutorBrainsDBEntities2())
{
var cl = new Client();
cl.ClientFirstName = "XXXXX";
cl.ClientLastName = "XXXXX";
tB.Clients.Add(cl); //Add this line to add your newly created clients to the collection
tB.SaveChanges();
}
Upvotes: 0
Reputation: 219057
You create the object, but you never actually add it to the data context. Something like this:
var cl = new Client();
cl.ClientFirstName = "XXXXX";
cl.ClientLastName = "XXXXX";
tb.Clients.Add(cl); // <-- add the entity to the context
tB.SaveChanges();
Otherwise there's nothing linking cl
to the data context, it's just an in-memory object like any other.
Upvotes: 1