gpt4 book ai didi

c# - 从 "Code First From Database"EF6 更改生成的类

转载 作者:行者123 更新时间:2023-12-02 00:38:28 25 4
gpt4 key购买 nike

我正在使用 EF6 从模型优先更改为代码优先我使用“Code First from Database”选项来生成我的类。

这些将与 WPF 应用程序一起使用。不幸的是,生成的项目没有 ObservableCollections 并且没有实现 INotiftyPropertyChanged。

我想知道是否有任何方法可以自动执行此操作(通过首先从数据库选项中选择代码时更改 c# 生成的类的行为。否则我将不得不手动进行更改,这将是非常乏味,因为我们有 100 多张 table 。

示例生成的类(请注意,属性和类未实现 INotiftyPropretyChanged,并且当我们需要 ObersvableCollections 时,ICollections 被初始化为 HashSets),这些要求适用于与 WPF/XAML 的数据绑定(bind)原因:

{
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 中选择的选项。 enter image description here

最佳答案

可在 MSDN 上获取说明:Customizing Code First to an Existing Database .

基本上您所做的就是添加 EntityFramework.CodeTemplates.CSharp将 NuGet 包(或 .VisualBasic,如果这是您的首选语言)添加到您的项目中,它会将向导使用的 T4 模板添加到项目中的 CodeTemplates\EFModelFromDatabase 文件夹中。

然后,您根据自己的喜好修改 T4 模板,并重新运行向导以从数据库重新生成模型。

关于c# - 从 "Code First From Database"EF6 更改生成的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27488059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com