我想保存我的编辑到数据库,我在ASP.NET MVC 3/C#中使用实体框架代码优先,但我收到错误.在我的Event类中,我有DateTime和TimeSpan数据类型,但在我的数据库中,我分别有日期和时间.这可能是原因吗?在保存对数据库的更改之前,如何在代码中转换为适当的数据类型.
public class Event
{
public int EventId { get; set; }
public int CategoryId { get; set; }
public int PlaceId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public DateTime EventDate { get; set; }
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
public string Description { get; set; }
public string EventPlaceUrl { get; set; }
public Category Category …Run Code Online (Sandbox Code Playgroud) 在Visual Studio中的调试会话中,我深入到Business层中的某个位置,试图找出实体在尝试持久化更改时表现异常的原因.
在调用堆栈中此处获取对此实体所属的DbContext的引用确实很有帮助.
即,看看这个实体的状态是什么(Unchanged,Modified等).
所以我正在寻找这样的辅助方法:
var db_context = DbContextHelpers.GetDbContext(entity);
// after that I could do something like this
var state = db_context.Entry(entity);
Run Code Online (Sandbox Code Playgroud)
我可以在调试期间在立即窗口中使用这些东西.
任何建议?
额外的笔记
实体必须知道DbContext某个地方,因为它正在使用它来延迟加载导航属性?
我已经阅读了很多关于WPF验证的博客文章DataAnnotations.我在想,如果有使用干净的方式DataAnnotations作为ValidationRules对我的实体.
所以没有这个(来源):
<Binding Path="Age" Source="{StaticResource ods}" ... >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
Run Code Online (Sandbox Code Playgroud)
你必须拥有你的
public class AgeRangeRule : ValidationRule
{...}
Run Code Online (Sandbox Code Playgroud)
我希望WPF Binding能够看到Age属性并查找DataAnnotation,如下所示:
[Range(1, 120)]
public int Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged<...>(x => x.Age);
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法,如果这是可能的?