[英]Linq to Sql General Help - Insert Statement
I am currently trying to create a new order (which will be shown below) in a web service, and then send that data to insert a new row into the database. 我目前正在尝试在Web服务中创建新订单(如下所示),然后发送该数据以在数据库中插入新行。 For some reason my DBML / Data Context does not allow me to use InsertOnSubmit.
由于某种原因,我的DBML /数据上下文不允许我使用InsertOnSubmit。
Any ideas? 有任何想法吗? I haven't used Linq to Sql in about 7 months.
我大约7个月没有用过Linq到Sql了。
Thanks in advance. 提前致谢。
[WebMethod]
public string InsertOrderToDatabases()
{
//Start Data Contexts ------
DataContext db = new DataContext(System.Configuration.ConfigurationManager.AppSettings["RainbowCMSConnectionString"]);
DataContext dcSqlOES = new DataContext(System.Configuration.ConfigurationManager.AppSettings["OESConnectionString"]);
//Get table from local database
Table<Schedule> Schedule = db.GetTable<Schedule>();
//Find last order number in databases
var lastOrderNumber = from lOrder in Schedule
orderby lOrder.templ_idn descending
select lOrder.templ_idn;
int firstOrderID;
var firstOrder = lastOrderNumber.FirstOrDefault();
firstOrderID = firstOrder.Value + 1;
qrOrder qrOrd = new qrOrder
{
.... data in here creating a new order
};
//TODO: fix below with an insert on submit
if (qrOrd != null)
{
// **Schedule.InsertOnSubmit(qrOrd);**
}
//db.GetTable<Schedule>().InsertOnSubmit(qrOrd);
try
{
//Submit the changes to the database
db.SubmitChanges();
return "Orders were sent to the databases.";
}
catch ()
{
}
}
Based on your response, it appears that you are using the wrong table, or perhaps the wrong data type. 根据您的响应,看来您使用的是错误的表或错误的数据类型。 I also noticed that when you declare your local
Schedule
variable, you declare it as type Table<Schedule>
, which means it should contain Schedule
entities, not qrOrder
entities. 我还注意到,在声明本地
Schedule
变量时,将其声明为Table<Schedule>
类型,这意味着它应包含Schedule
实体,而不是qrOrder
实体。
Table<TEntity>.InsertOnSubmit
expects a specific strongly typed entity to be passed in. In your case, it is expecting Web_Service.Schedule
, but you are trying to pass in a qrOrder
. Table<TEntity>.InsertOnSubmit
期望传递一个特定的强类型实体。在您的情况下,它期望的是Web_Service.Schedule
,但是您试图传递一个qrOrder
。
Schedule.InsertOnSubmit(qrOrd);
That line will not treat to submit changes to connected entity , Try this 该行不会将更改提交给关联实体,请尝试此操作
db.Schedule.InsertOnSubmit(qrOrd);
db.SubmitChanges();
you can try with 你可以尝试
db.GetTable(typeof(Schedule)).InsertOnSubmit(qrOrd);
Or 要么
db.GetTable(qrOrd.GetType()).InsertOnSubmit(qrOrd);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.