简体   繁体   中英

ADO.NET Entity Model: cannot insert into db programatically

I am using:

  1. Windows 10
  2. Visual C# Express 2010
  3. SQL Server 2012 Express, but it won't integrate properly with VS.

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)

Database Explorer

Data Source

I've tried various ways of coding an INSERT which simply ends in SaveChanges() , eg

 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 (eg a link to an article covering this rather weird problem.)

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.

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();
 }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM