我正在尝试使用DBContext的ChangeTracker对象实现AuditLog,我遇到了一个问题,在这个问题上,它DbEntityEntry.OriginalValues被删除并被替换为DbEntityEntry.CurrentValues.我注意到问题是我如何更新DbContext中正在跟踪的对象(原始帖子:实体框架DbContext SaveChanges()OriginalValue不正确).
所以现在我需要一些帮助,以正确的方式使用MVC 3中的存储库模式更新持久化对象与实体框架4.此示例代码改编自Apress推出的Pro Asp.NET MVC 3框架书中的SportsStore应用程序.
这是我在AdminController中的'编辑'帖子操作:
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
// Here is the line of code of interest...
repository.SaveProduct(product, User.Identity.Name);
TempData["message"] = string.Format("{0} has been saved", product.Name);
return RedirectToAction("Index");
}
else
{
// there is something wrong with the data values
return View(product);
}
}
Run Code Online (Sandbox Code Playgroud)
这将调用具体类EFProductRepository(它实现IProductRepository接口并通过Ninject注入).以下是SaveProduct具体存储库类中的方法:
public void SaveProduct(Product product, string userID)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
else
{ …Run Code Online (Sandbox Code Playgroud) XDocument.Load(XmlReader)调用时可能抛出的异常有哪些?当文档无法提供关键信息时,很难遵循最佳实践(即避免使用通用的try catch块).
提前感谢你的帮助.
我有一个从字节数组创建的XDocument(通过tcp/ip接收).
然后我搜索特定的xml节点(XElements),并在通过调用XElement.Remove()从Xdocument中检索值'pop'之后.在我的所有解析完成后,我希望能够记录我没有解析的xml(XDocument中的剩余xml).问题是在调用XElement.Remove()时会留下额外的空格.我想知道删除这个额外空格的最佳方法,同时保留剩余xml中的其余格式.
示例/示例代码
如果我通过套接字收到以下xml:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
Run Code Online (Sandbox Code Playgroud)
我使用以下代码来解析此xml并删除一些XElements:
private void socket_messageReceived(object sender, MessageReceivedEventArgs e)
{
XDocument xDoc;
try
{
using (MemoryStream xmlStream = new MemoryStream(e.XmlAsBytes))
using (XmlTextReader reader = new XmlTextReader(xmlStream))
{
xDoc = XDocument.Load(reader);
}
XElement Author = xDoc.Root.Descendants("author").FirstOrDefault();
XElement Title = xDoc.Root.Descendants("title").FirstOrDefault();
XElement Genre = xDoc.Root.Descendants("genre").FirstOrDefault();
// Do something with Author, Title, and Genre here...
if (Author != …Run Code Online (Sandbox Code Playgroud) 我试图通过覆盖SaveChanges()方法使用EF 4.1实现AuditLog,如下所述:
我遇到了"修改"条目的问题.每当我尝试获取相关属性的OriginalValue时,它总是具有与CurrentValue字段中相同的值.
我首先使用此代码,并成功识别修改的条目:
public int SaveChanges(string userID)
{
// Have tried both with and without the following line, and received same results:
// ChangeTracker.DetectChanges();
foreach (
var ent in this.ChangeTracker
.Entries()
.Where(p => p.State == System.Data.EntityState.Added
p.State == System.Data.EntityState.Deleted
p.State == System.Data.EntityState.Modified))
{
// For each change record, get the audit record entries and add them
foreach (AuditLog log in GetAuditRecordsForChange(ent, userID))
{
this.AuditLog.Add(log);
}
}
return base.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
问题在于此(缩写代码):
private List<AuditLog> …Run Code Online (Sandbox Code Playgroud) 我遇到了众所周知的问题,在将TreeNode的字体设置为粗体后,TreeNode的文本被截断.但是,我相信我发现了一种所有普遍接受的"修复"都无法工作的情况.
常见解决方案: http ://support.microsoft.com/kb/937215
node.NodeFont = new System.Drawing.Font(node.NodeFont, FontStyle.Bold);
node.Text += string.Empty;
Run Code Online (Sandbox Code Playgroud)
变体1: C#Winforms粗体树视图节点不显示全文(参见BlunT的答案)
node.NodeFont = new System.Drawing.Font(node.NodeFont, FontStyle.Bold);
node.Text = node.Text;
Run Code Online (Sandbox Code Playgroud)
变体2: http ://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/acb877a6-7c9d-4408-aee4-0fb7db127934
node.NodeFont = new System.Drawing.Font(node.NodeFont, FontStyle.Bold);
treeView1.BackColor = treeView1.BackColor;
Run Code Online (Sandbox Code Playgroud)
以上修复不起作用的场景:
如果将节点设置为粗体的代码位于构造函数中(表单中的一个,或者在本例中为用户控件),则修复程序将不起作用:
public partial class IncidentPlanAssociations : UserControl
{
public IncidentPlanAssociations()
{
InitializeComponent();
TreeNode node = new TreeNode("This is a problem.");
node.NodeFont = new Font(treeView1.Font, FontStyle.Bold);
treeView1.Nodes.Add(node);
// This does not fix problem
node.Text += string.Empty;
// This does not fix problem
node.Text = …Run Code Online (Sandbox Code Playgroud) 我是软件开发的新手,也是stackoverflow的新手,所以对我来说很容易.
背景:我正在开发一个C#类库,它通过tcp/ip处理第三方应用程序发送的xml消息(使用异步套接字).我正在使用com-interop将类库公开给Vb6应用程序.当C#库处理它通过套接字接收的xml时,它会引发消费vb6应用程序订阅的各种事件(这样,当我们最终在.Net中重写整个应用程序时,我们已经完成了这个组件).
问题:我想捕获所有仅用于登录的未处理异常.在winforms应用程序中,您可以将事件连接到AppDomain.CurrentDomain.UnhandledException和Application.ThreadException.有没有办法同样抓取异常数据来记录类库中的信息?
重点:
我不是试图从这些异常中恢复,而只是记录它们并让异常传播并在需要时崩溃应用程序.
我正在尽力在本地捕获所有特定异常,无论我知道它们可能发生在哪里.因此,我的目的是记录真正意外的异常.
任何人都可以为我提供一些方向吗?到目前为止,我发现的最佳选项似乎是在每个公共方法中放置一个通用的try catch块,记录异常,然后抛出它.这似乎不太理想:
public void SomeMethod()
{
try
{
// try something here...
}
catch (Exception ex)
{
Log(ex);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
这不仅看起来像一个糟糕的设计,而且我不知道如果其中一个异步回调在不同的线程上导致异常而不是调用该方法会发生什么.这个通用的try/catch块是否仍会捕获这样的异常?
谢谢你的帮助.
编辑:我最初标记@Eric J.的答案是正确的,但在尝试实现解决方案后,我发现它不能很好地处理我正在使用的套接字类的异步回调.一旦使用线程池线程来触发异步回调,我似乎无法捕获堆栈中稍后发生的任何异常.我是否需要使用AOP框架,还是有其他方法来捕获这些异常?
c# multithreading class-library com-interop unhandled-exception
我们有一个项目,它设置组合框的数据源,但允许用户从此列表中选择某些内容或键入列表中未包含的项目。基本上,有一个包含街道的地理数据库,但用户不需要从列表中选择街道。ComboBox.DropDownStyle 设置为 DropDown。
如果用户编辑的记录包含不在地理数据库中(因此不在 ComboBox.DataSource 中)的街道,我们在正确填充表单时会遇到问题。
这是我们问题的一个大大简化的形式:
private void button1_Click(object sender, EventArgs e)
{
// Create a new form. In the constructor the DataSource of the ComboBox is set with three items:
// Main St., First St., and Second St.
ComboBoxTrialForm frm = new ComboBoxTrialForm();
// Set comboBox1.Text equal to an item NOT in the datasource
frm.SetComboTextValue("Michigan Ave.");
// Show the form, and the comboBox has the first item in its datasource selected
frm.Show();
}
Run Code Online (Sandbox Code Playgroud)
ComboBoxTrial 类是这样的:
public partial class …Run Code Online (Sandbox Code Playgroud) 我有一个C#dll通过com-interop暴露给vb6.这一切都有效,但是当我将.Net的自定义对象数组传递给VB6时,我注意到了一些奇怪的事情.
从VB6访问数组让我感到困惑.如果我直接访问数组,我必须这样做:
Dim manager as New ObjectManager
'Access with two sets of parentheses:
msgbox manager.ReturnArrayOfObjects()(0).Name
Run Code Online (Sandbox Code Playgroud)
但是,如果我先复制数组,我可以访问它通常所期望的:
Dim manager as New ObjectManager
Dim objectArray() As CustomObject
'copy the array
objectArray = manager.ReturnArrayOfObjects
'access normally:
msgbox objectArray(0).Name
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,我不得不用2套括号:manager.ReturnArrayOfObjects()(0).Name 在第二种情况下,我可以只使用一个括号中:objectArray(0).Name
有谁知道为什么会这样?我可能在这里做错了吗?
这是C#interop代码的快速存根/示例.
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("[Guid here...]")]
public interface IObjectManager
{
[DispId(1)]
CustomObject[] ReturnArrayOfObjects();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("[guid here...]")]
public class ObjectManager: IObjectManager
{
public CustomObject[] ReturnArrayOfObjects()
{
return new CustomObject[] { new CustomObject(), …Run Code Online (Sandbox Code Playgroud) c# ×8
com-interop ×2
linq-to-xml ×2
winforms ×2
xml ×2
.net ×1
arrays ×1
data-binding ×1
exception ×1
treeview ×1
vb6 ×1
xelement ×1