小编Ami*_*san的帖子

更新行时如何解决DbUpdateConcurrencyException?

我正在使用ASP.NET MVC应用程序的实体框架代码第一种方法.在提交保存更改时编辑一行后,http post方法出现以下错误:

类型"System.Data.Entity.Infrastructure.DbUpdateConcurrencyException"的异常出现在EntityFramework.dll但在用户代码中没有处理.

db.SaveChanges()遇到此错误.

db.Entry<Project>(EditedProj).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

主要代码:

[HttpGet]
public ActionResult Edit(int id)
{
    using (var db = new ProjectContext())
    {
        return View(db.Projects.Find(id));
    }
}

[HttpPost]
public ActionResult Edit(Project EditedProj)
{
    using (var db = new ProjectContext())
    {
        db.Entry<Project>(EditedProj).State = 
             System.Data.Entity.EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Projects");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net entity-framework

12
推荐指数
1
解决办法
3万
查看次数

为什么反思依赖注入?

编辑:我正在学习依赖注入.我理解在开发大型系统时它至关重要的基本原因.

似乎依赖注入使用反射来实现其一些目标.反思是一种逆向工程(可能只对我而言).

如果有人解释为什么以及如何使用Reflection以及它是否是可选的,那将对我非常有帮助.

c# dependency-injection

6
推荐指数
1
解决办法
4426
查看次数

FontSize 未在 Contentpresenter 和 ContentControl 中继承的解决方法

试图使 UserControl 可以承载其他控件。以下是相关代码。

<UserControl … … … … >
  <Grid DataContext="{Binding RelativeSource={RelativeSource 
            Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
      … … …
     <ContentPresenter Content="{Binding SomeContent}"/>
      … … …
  </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

并使用此 UserControl 如下 -

<myCtrl:ContainerUserControl FontSize="18pt">
    <myCtrl:ContainerUserControl.SomeContent>
        <Grid>
            <TextBox Text="Hello World"/>
        </Grid>
    </myCtrl:ContainerUserControl.SomeContent>
</myCtrl:ContainerUserControl >
Run Code Online (Sandbox Code Playgroud)

问题是 FontSize 没有继承到 TextBox。我可以将 FontSize 设置为 TextBox 但这不是一个优雅的解决方案。我试过使用 ContentControl 但没有改变。也尝试使用

<ContentPresenter TextElement.FontSize="{Binding FontSize}" Content="{Binding SomeContent}"/>
Run Code Online (Sandbox Code Playgroud)

也不行。FontSize 并不是我唯一担心的事情。我可能还需要其他属性来继承。

可以做些什么来解决这个问题?

wpf xaml

5
推荐指数
1
解决办法
902
查看次数

是否可以使用路由属性仅接受路由参数的特定值?

下面的代码做了我想做的事情。如果字符串参数ForSaleOrRent有任何值,但ForSaleForRent操作方法返回HttpNotFoundResult。我的问题是仅使用 Route 属性可以吗?

[Route("post/{ForSaleOrRent}")]
public ActionResult PostProperty(string ForSaleOrRent)
{
    bool IsValidUrl = ForSaleOrRent.ToUpper() == "FORSALE" || 
                      ForSaleOrRent.ToUpper() == "FORRENT";
    if (!IsValidUrl)
    {
        return new HttpNotFoundResult();
    }
    return View(ForSaleOrRent);
}
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc url-routing asp.net-mvc-routing attributerouting asp.net-mvc-5

3
推荐指数
1
解决办法
1494
查看次数