从“从数据库代码优先”改变生成的类。 EF6 [英] Changing the generated classes from "Code First From Database" EF6

查看:70
本文介绍了从“从数据库代码优先”改变生成的类。 EF6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF6
从模型优先转换为代码优先,我正在使用从数据库优先编码选项生成我的类。

I'm changing from model first to code first with EF6 I'm using the Code first from Database option to generate my classes.

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

These are going to be used with a WPF application. Unfortunately the items generated do not have ObservableCollections and don't implement INotiftyPropertyChanged.

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

I'm wondering is there any way to automate this (by changing the behaviour of the classes c# generates when selecting the code first from DB option. Otherwise I'll have to go through and make the changes by hand which will be very tedious as we have over 100 tables.

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

Sample generated Class (note the properties and class don't implement INotiftyPropretyChanged and the ICollections are initialised as HashSets, when we'd like ObersvableCollections), these requirements are for data binding with WPF/XAML reasons:

{
    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));

        }
    }
}

选项I在Visual Studio中选择。

Option I'm selecting in visual studio.

推荐答案

MSDN上的说明位于首先将代码自定义为现有代码数据库

Instructions are available on MSDN at Customizing Code First to an Existing Database.

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

Basically all you do is add the EntityFramework.CodeTemplates.CSharp NuGet package (or .VisualBasic if that's your preferred language) to your project, and it will add the T4 templates used by the wizard to the CodeTemplates\EFModelFromDatabase folder in your project.

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

Then you modify the T4 templates to your liking and re-run the wizard to regenerate the model from your database.

这篇关于从“从数据库代码优先”改变生成的类。 EF6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆