C# 将生成的类从";“首先从数据库中输入代码”;EF6
我正在使用EF6从模型优先改为代码优先 我使用“代码优先于数据库”选项来生成我的类 这些将与WPF应用程序一起使用。 不幸的是,生成的项没有ObservableCollections,并且没有实现InotifyPropertyChanged 我想知道是否有任何方法可以自动化这一点(通过改变c#在从DB选项中选择代码时生成的类的行为。否则,我将不得不通过手工进行更改,这将非常繁琐,因为我们有100多个表 示例生成的类(请注意,属性和类未实现InotifyPropretyChanged,当我们需要ObersvableCollections时,ICollections被初始化为哈希集),这些要求用于与WPF/XAML的数据绑定,原因如下:C# 将生成的类从";“首先从数据库中输入代码”;EF6,c#,wpf,entity-framework,C#,Wpf,Entity Framework,我正在使用EF6从模型优先改为代码优先 我使用“代码优先于数据库”选项来生成我的类 这些将与WPF应用程序一起使用。 不幸的是,生成的项没有ObservableCollections,并且没有实现InotifyPropertyChanged 我想知道是否有任何方法可以自动化这一点(通过改变c#在从DB选项中选择代码时生成的类的行为。否则,我将不得不通过手工进行更改,这将非常繁琐,因为我们有100多个表 示例生成的类(请注意,属性和类未实现InotifyPropretyChanged,当我们需要O
{
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; }
}
}
{
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity.Spatial;
[表(“终端会话”)]
公共部分类终端会话
{
公共终端会话()
{
ternalcheckpoints=新HashSet();
TerminalFloats=newhashset();
TerminalTransactions=新哈希集();
}
public int TerminalSessionID{get;set;}
公共int?TerminalID{get;set;}
公共日期时间?开始日期{get;set;}
公共日期时间?结束日期{get;set;}
公共虚拟ICollection终端检查点{get;set;}
公共虚拟ICollection TerminalFloats{get;set;}
公共虚拟ICollection 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));
}
}
}
{
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity.Spatial;
[表(“终端会话”)]
公共分部类TerminalSession:INotifyPropertyChanged
{
私人国际终端会话ID;
专用int?\u终端;
私人日期时间?\u开始日期;
私人日期时间?\u endDate;
公共终端会话()
{
TerminalCheckpoints=新的ObservableCollection();
TerminalFloats=新的ObservableCollection();
TerminalTransactions=新的ObservableCollection();
}
公共int终端会话ID
{
获取{return\u terminalSessionId;}
设置
{
if(value==\u terminalSessionId)返回;
_terminalSessionId=值;
OnPropertyChanged();
_terminalSessionId=值;
}
}
公共int?终端
{
获取{return\u terminalId;}
设置
{
if(value==\u terminalId)返回;
_terminalId=值;
OnPropertyChanged();
_terminalId=值;
}
}
公共日期时间?起始日期
{
获取{return\u startDate;}
设置
{
if(value==\u startDate)返回;
_startDate=值;
OnPropertyChanged();
_startDate=值;
}
}
公共日期时间?结束日期
{
获取{return\u endDate;}
设置
{
如果(值==\u结束日期)返回;
_endDate=值;
OnPropertyChanged();
_endDate=值;
}
}
公共虚拟可观察集合终端检查点{get;set;}
公共虚拟ObservableCollection TerminalFloats{get;set;}
公共虚拟ObservableCollection TerminalTransactions{get;set;}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
我在VisualStudio中选择的选项。
您可以在这里找到解决方案: 它在那部分 “正在更新数据绑定的代码生成”
有关说明,请访问MSDN上的 基本上,您只需将NuGet软件包(或.VisualBasic,如果这是您的首选语言)添加到项目中,它就会将向导使用的T4模板添加到项目中的
code模板\EFModelFromDatabase
文件夹中
然后,您可以根据自己的喜好修改T4模板,并重新运行向导以从数据库中重新生成模型。我认为Brian Hinchley关于这个问题的回答也适用于此处更改的InotifyProperty(该问题仅与早期版本的EF相似,EF 4.(有点)
如果可以的话,我将修改我的答案,提供更多关于如何执行可观察收集部分的详细信息和说明(现在就测试它)。我认为只有先使用数据库才能做到这一点(我先使用代码,然后从现有数据库中生成代码)。您可以使用一些信息,比如创建一个具有通用功能的基类,以便从中继承模型,但您必须从头开始更改自己的模板。与我在以前版本中看到的代码相比,它们在EF6中看起来非常不同。@afrazier,我几乎可以使用它,但无法使用它使用_camelCase必须为一个名为camelCase的属性生成我的字段,而不是我希望其他一切都正常工作。回答得很好。但是,遗憾的是,没有给出如何生成的指导