- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习本教程 Creating Master-Details Windows Forms with the Entity Framework但是如果我修改了某些列或添加了新行,db.Savechanges()
将不起作用。根据这个视频教程,它会起作用,但它不起作用。
Product_Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RemoteSynchronizer
{
public partial class Product : Form
{
ProductsEntities db = new ProductsEntities();
public Product()
{
InitializeComponent();
}
private void Product_Form_Load(object sender, EventArgs e)
{
product_DBindingSource.DataSource = db.Product_D.ToList();
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
}
private void product_DBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
// Product_D obj = new Product_D();
// obj.PCode = pCodeTextBox.Text;
// obj.Name = nameTextBox.Text;
// obj.Batch = batchTextBox.Text;
// obj.Expiry = expiryTextBox.Text;
// obj.Price = priceTextBox.Text;
//try
// {
// Product_D temp = db.Product_D.Single(t => t.PCode == pCodeTextBox.Text);
// MessageBox.Show("Product Exists");
// }
// catch
// {
// db.Product_D.Add(obj);
db.SaveChanges();
//}
}
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
}
}
}
当我尝试在代码中使用注释方法时出现错误
错误
************** Exception Text **************
System.Data.Entity.Infrastructure.DbUpdateException: Unable to update the EntitySet 'Product_D' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation. ---> System.Data.UpdateException: Unable to update the EntitySet 'Product_D' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
at System.Data.SqlClient.SqlGen.DmlSqlGenerator.ExpressionTranslator.Visit(DbScanExpression expression)
at System.Data.Common.CommandTrees.DbScanExpression.Accept(DbExpressionVisitor visitor)
at System.Data.SqlClient.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree tree, SqlVersion sqlVersion, List`1& parameters)
at System.Data.SqlClient.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, SqlVersion sqlVersion, List`1& parameters, CommandType& commandType, HashSet`1& paramsToForceNonUnicode)
at System.Data.SqlClient.SqlProviderServices.CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
at System.Data.SqlClient.SqlProviderServices.CreateCommand(DbCommandTree commandTree)
at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at RemoteSynchronizer.Product.product_DBindingNavigatorSaveItem_Click(Object sender, EventArgs e) in e:\Study\Project\RemoteSynchronizer\RemoteSynchronizer\Product_Form.cs:line 50
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18010 built by: FX45RTMGDR
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
RemoteSynchronizer
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///E:/Study/Project/RemoteSynchronizer/RemoteSynchronizer/bin/Debug/RemoteSynchronizer.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18016 built by: FX45RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Web
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Web/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
EntityFramework
Assembly Version: 5.0.0.0
Win32 Version: 5.0.20627.0
CodeBase: file:///E:/Study/Project/RemoteSynchronizer/RemoteSynchronizer/bin/Debug/EntityFramework.DLL
----------------------------------------
System.Data.Entity
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.Entity/v4.0_4.0.0.0__b77a5c561934e089/System.Data.Entity.dll
----------------------------------------
Anonymously Hosted DynamicMethods Assembly
Assembly Version: 0.0.0.0
Win32 Version: 4.0.30319.18010 built by: FX45RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
----------------------------------------
Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Transactions
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------
System.Data.OracleClient
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data.OracleClient/v4.0_4.0.0.0__b77a5c561934e089/System.Data.OracleClient.dll
----------------------------------------
System.Xml.Linq
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
EntityFrameworkDynamicProxies-RemoteSynchronizer
Assembly Version: 1.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.Entity/v4.0_4.0.0.0__b77a5c561934e089/System.Data.Entity.dll
----------------------------------------
System.Runtime.Serialization
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
----------------------------------------
System.ComponentModel.DataAnnotations
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.17929
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ComponentModel.DataAnnotations/v4.0_4.0.0.0__31bf3856ad364e35/System.ComponentModel.DataAnnotations.dll
----------------------------------------
Microsoft.SqlServer.Types
Assembly Version: 11.0.0.0
Win32 Version: 2011.0110.2100.060 ((SQL11_RTM).120210-1917 )
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.SqlServer.Types/11.0.0.0__89845dcd8080cc91/Microsoft.SqlServer.Types.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
如何摆脱这个问题?我试图通过简单的方法 db.savechanges()
解决这个问题,但它不起作用。
最佳答案
Unable to update the EntitySet because it has a DefiningQuery and no element exists in the element to support the current operation.
确保您的表有主键!
如果没有添加主键并更新 Entity Framework 模型并再次运行。
关于c# - db.savechanges() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13263557/
我一直想知道的一个问题是,我应该显式调用 context.SaveChanges() 还是让它在 Dispose() 下。 方法 1:自动保存 public virtual void Disp
我正在使用EF5和“数据库优先”方法在WPF中编写第一个MVVM应用程序。 我在MVVM中的模型是EF5为我生成的(我确实需要自定义T4模板以包括INotifyPropertyChanged)。 一切
我使用 Visual Studio 在 ASP.NET MVC 项目中处理本地数据库。 我在修改数据库时遇到问题。当我调用 SaveChanges() 时,我总是会收到此错误: The value c
我正在使用 Entity Framework 同时插入对象,如下所示。 context = new MyContext(); foreach (var x in lstX) { var abc
我在 ObjectSet 中添加数据并在 ObjectContext 上执行了 SaveChanges。但是新数据没有显示在 DataGrid 中! 代码: bsWork.DataSource = P
我正在使用 Oracle 数据库和 ASP.net MVC4 创建一个应用程序。即使在代码中看起来没有问题,调用 SaveChanges()方法导致此处显示的错误: 这张图片显示了内部异常和相关细节
我刚开始使用 Entity Framework ,我能够使用 linq 进行选择操作,但我在插入时遇到了问题。我试过这个示例,看看有什么问题: testEntities te = ne
假设我在 Controller 中调用了这样的东西: using (var context = new SqlContext()) { context.Items.Add(new Item("
我的 Asp.net mvc web 应用程序中有以下 Action 方法:- [HttpPost] [ValidateAntiForgeryToken] public ActionResult Cr
目标框架:netstandard2.0 Entity Framework 核心:2.2.6 我在 OnModelCreating 中有以下代码: protected override void OnM
这个问题在这里已经有了答案: DbContext SaveChanges Order of Statement Execution (1 个回答) 7年前关闭。 当上下文包含相关实体和 SaveCha
我有一个使用 Entity Framework 6.1、代码优先创建的 SQL-Azure 数据库。 我的“EmazeEvents”表中的“日期时间”字段是这样创建的: datetime = c.Da
查看人们编写的示例,我发现很多人使用 SaveChanges 而不是使用 SaveChangesWithRetries。我认为 SaveChangesWithRetries 是最好的选择,那么仅使用
RavenDB 遇到一个奇怪的问题 public ActionResult Save(RandomModel model) { //Do some stuff, validate model etc.
有没有办法保存单个跟踪对象的更改,而不是 ObjectStateManager 中的所有对象,我的意思是: ObjectContext.SaveChanges(Contact) 最佳答案 也许您可以创
查看人们编写的示例,我发现很多人使用 SaveChanges 而不是使用 SaveChangesWithRetries。我认为 SaveChangesWithRetries 是最好的选择,那么仅使用
我正在尝试编写一个生成多张发票的方法。这是一所大学,其中客户在名为 Enrollments 的类(class)中与导师一起注册。通过这种方法,我试图将导师客户的月费乘以他们的佣金百分比,因为导师从
为什么 DbContext ctx 在每次 SaveChanges() 执行后处理得更快? 第一个样本: var ctx = new DomainContext(); foreach (var ite
我一直在玩 Entity Core 类,例如 DbContext,在尝试保存对象时遇到以下错误: An error occurred while saving entities that do not
场景如下 - 不同的用户通过从网页的下拉列表中选择一个值来进行更改。下拉列表包含在 DataView 中或通过构建表格。如果用户 A 对第 1 行进行了更改,它会更新数据库并在重新绑定(bind)后显
我是一名优秀的程序员,十分优秀!