从“代码优先自数据库” EF6更改生成的类 - c#

我正在使用EF6从模型优先转换为代码优先
我正在使用“首先从数据库中选择代码”选项来生成我的类。

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

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

示例生成的类(请注意,属性和类未实现INotiftyPropretyChanged,并且当我们需要ObersvableCollections时,将ICollections初始化为HashSets),这些要求是出于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中选择的选项。

参考方案

有关说明,请参见MSDN上的Customizing Code First to an Existing Database。

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

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

从列表中获取价值(C#UWP) - c#

我有课 public class RootObject { public int id { get; set; } public int parent_id { get; set; } public string status { get; set; } public string order_key { get; set; } public string …

.get()之后,多处理陷入困境 - python

我试图了解multiprocessing如何在python中工作并遇到一些问题。这是示例:import multiprocessing def func(): return 1 p = multiprocessing.Pool() result = p.apply_async(func).get() 调用.get()函数时,代码只是卡住了。我究竟做错了什么?…

AJAX和file_get_contents - php

我有一个使用file_get_contents的脚本,然后解码返回的JSON ...$urlone = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=f…

下载file_get_contents响应 - php

我有以下jQuery:$(".download").click(function(){ $.post('get_bot.php', "url_code="+url_code, function (response) { alert(response); }); }); url_code是一个具有js…

如何在属性中搜索特定值? - c#

我有以下实体框架模型:public class Person { public int Id { get; set; } public string Name { get; set; } public List<Email> EmailAddresses { get; set; } } public class Email { public in…