简体   繁体   English

ADO.NET实体模型:无法以编程方式插入db

[英]ADO.NET Entity Model: cannot insert into db programatically

I am using: 我在用:

  1. Windows 10 Windows 10
  2. Visual C# Express 2010 Visual C#Express 2010
  3. SQL Server 2012 Express, but it won't integrate properly with VS. SQL Server 2012 Express,但无法与VS正确集成。

I have now recreated my database in VS 2010 as a .fsi (not .msi) database. 现在,我已经在VS 2010中将数据库重新创建为.fsi(不是.msi)数据库。

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) 我已经设置了数据源和连接(到完全用VS构建的数据库-甚至不知道它是否正在使用仍受支持的SQL引擎)

Database Explorer 数据库浏览器

Data Source 数据源

I've tried various ways of coding an INSERT which simply ends in SaveChanges() , eg 我尝试了多种编码INSERT的方法,这些方法SaveChanges()结尾,例如

 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. 我还在类中using System.Data.EntityModel ,在解决方案中也有对System.Data.Entity的引用。

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.) 知道这不是最好的一天,但是我在这里承受着很大的压力,并且不想使用一些过于复杂的Excel文件。...因此,我们将不胜感激(例如链接到涵盖这个相当奇怪的问题的文章。)

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. 否则,没有将cl链接到数据上下文的方法,它只是一个内存对象,就像其他对象一样。

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 如果您使用的是EF(主要是数据库优先方法),则您的数据库上下文类已经具有Clients实体集合的引用,在这种情况下,

 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 只需将数据库中的ClientID设置为SSMS中的标识列,然后尝试

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. 我试图使用.Add(),但它不是有效选项。

Nor can I manually enter rows into the tables by typing them in & using the Run SQL command. 我也无法通过使用“运行SQL”命令在&中键入行来将它们手动输入到表中。 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!! 由于它已将db文件存储在根文件夹和调试文件夹中,有人说这很常见,并将属性更改为“从不复制”或其他内容-无论如何,我将在2016年完成它! and hopefully something will work. 希望有什么会起作用。

Best, D 最好,D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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