【发布时间】:2014-12-15 15:58:07
【问题描述】:
我正在使用 EF6 从模型优先更改为代码优先 我使用 Code first from Database 选项来生成我的类。
这些将与 WPF 应用程序一起使用。 不幸的是,生成的项目没有 ObservableCollections,也没有实现 INotiftyPropertyChanged。
我想知道是否有任何方法可以自动执行此操作(通过更改 C# 在首先从 DB 选项中选择代码时生成的类的行为。否则我将不得不手动进行更改,这将是非常乏味,因为我们有 100 多张桌子。
示例生成的类(注意属性和类不实现 INotiftyPropretyChanged 并且 ICollections 被初始化为 HashSet,当我们想要 ObersvableCollections 时),这些要求适用于与 WPF/XAML 的数据绑定原因:
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("TerminalSession")]
public partial class TerminalSession
{
public TerminalSession()
{
TerminalCheckpoints = new HashSet<TerminalCheckpoint>();
TerminalFloats = new HashSet<TerminalFloat>();
TerminalTransactions = new HashSet<TerminalTransaction>();
}
public int TerminalSessionID { get; set; }
public int? TerminalID { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public virtual ICollection<TerminalCheckpoint> TerminalCheckpoints { get; set; }
public virtual ICollection<TerminalFloat> TerminalFloats { get; set; }
public virtual ICollection<TerminalTransaction> TerminalTransactions { get; set; }
}
}
我们真正想要的代码:
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("TerminalSession")]
public partial class TerminalSession : INotifyPropertyChanged
{
private int _terminalSessionId;
private int? _terminalId;
private DateTime? _startDate;
private DateTime? _endDate;
public TerminalSession()
{
TerminalCheckpoints = new ObservableCollection<TerminalCheckpoint>();
TerminalFloats = new ObservableCollection<TerminalFloat>();
TerminalTransactions = new ObservableCollection<TerminalTransaction>();
}
public int TerminalSessionID
{
get { return _terminalSessionId; }
set
{
if (value == _terminalSessionId) return;
_terminalSessionId = value;
OnPropertyChanged();
_terminalSessionId = value;
}
}
public int? TerminalID
{
get { return _terminalId; }
set
{
if (value == _terminalId) return;
_terminalId = value;
OnPropertyChanged();
_terminalId = value;
}
}
public DateTime? StartDate
{
get { return _startDate; }
set
{
if (value == _startDate) return;
_startDate = value;
OnPropertyChanged();
_startDate = value;
}
}
public DateTime? EndDate
{
get { return _endDate; }
set
{
if (value == _endDate) return;
_endDate = value;
OnPropertyChanged();
_endDate = value;
}
}
public virtual ObservableCollection<TerminalCheckpoint> TerminalCheckpoints { get; set; }
public virtual ObservableCollection<TerminalFloat> TerminalFloats { get; set; }
public virtual ObservableCollection<TerminalTransaction> TerminalTransactions { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我在 Visual Studio 中选择的选项。
【问题讨论】:
标签: c# wpf entity-framework