简体   繁体   English

实体框架DB-First - 我可以从基础模型继承以添加我的功能吗?

[英]Entity Framework DB-First - can I inherit from base model in order to add my functionality?

I'm creating a simple application in C#. 我在C#中创建一个简单的应用程序。 One of it's features is adding invoices. 其中一项功能是添加发票。

For this purpose I've created two tables in my database - Invoice and InvoiceLine, and used Entity Framework to map them for me. 为此,我在我的数据库中创建了两个表 - Invoice和InvoiceLine,并使用Entity Framework为我映射它们。 I needed to add some functionality for the InvoiceLine class, but I know you shouldn't edit the code created by Entity Framework. 我需要为InvoiceLine类添加一些功能,但我知道你不应该编辑Entity Framework创建的代码。 Instead, I decided to inherit from the class created by EF and add this functionality ontop of this class. 相反,我决定继承自EF创建的类,并在此类的顶部添加此功能。

I did this, but now, when I'm trying to save it to the database, I get an error - 'System.InvalidOperationException: 'Mapping and metadata information could not be found for EntityType 'InvoiceRegister.Models.InvoiceLineModel'. 我这样做了,但是现在,当我试图将它保存到数据库时,我收到一个错误 - 'System.InvalidOperationException:'无法找到EntityType'InvoiceRegister.Models.InvoiceLineModel'的映射和元数​​据信息。

My question is - how can it be fixed? 我的问题是 - 如何解决? Also, is there a better way to achieve what I wanted? 另外,有没有更好的方法来实现我想要的? I wanted to do my project in MVVM, so I wanted to create a Model, and not do the functionality in my ViewModel. 我想在MVVM中完成我的项目,所以我想创建一个Model,而不是在我的ViewModel中执行该功能。

Code: 码:

Original class (InvoiceLine): 原始类(InvoiceLine):

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace InvoiceRegister.Models
{
    using System;
    using System.Collections.Generic;

    public partial class InvoiceLine
    {
        public int DocEntry { get; set; }
        public int LineId { get; set; }
        public int LineNum { get; set; }
        public string ItemName { get; set; }
        public double Amount { get; set; }
        public int TaxPrc { get; set; }
        public decimal NetAmnt { get; set; }
        public decimal TaxAmnt { get; set; }
        public decimal TotalAmnt { get; set; }
        public decimal Price { get; set; }

        public virtual Invoice Invoice { get; set; }
    }
}

My class. 我的课。 I want it to inherit from InvoiceLine: 我希望它继承InvoiceLine:

namespace InvoiceRegister.Models
{
    public class InvoiceLineModel : InvoiceLine, INotifyPropertyChanged
    {
        private double _amount;

        public new double Amount
        {
            get { return _amount; }
            set
            {
                _amount = value;
                OnPropertyChanged("Amount");
                OnPropertyChanged("NetAmnt");
                OnPropertyChanged("TaxAmnt");
                OnPropertyChanged("TotalAmnt");
            }
        }

        private decimal _price;

        public new decimal Price
        {
            get { return _price; }
            set
            {
                _price = value;
                OnPropertyChanged("Price");
                OnPropertyChanged("NetAmnt");
                OnPropertyChanged("TaxAmnt");
                OnPropertyChanged("TotalAmnt");
            }
        }

        private int _taxPrc;

        public new int TaxPrc
        {
            get { return _taxPrc; }
            set
            {
                _taxPrc = value;
                OnPropertyChanged("TaxPrc");
                OnPropertyChanged("TaxAmnt");
                OnPropertyChanged("TotalAmnt");
            }
        }


        public new decimal TaxAmnt
        {
            get
            {
                return NetAmnt* TaxPrc/100;
            }
        }


        public new decimal NetAmnt
        {
            get
            {
                return  (decimal)Amount * Price;
            }
        }

        public new decimal TotalAmnt
        {
            get {return NetAmnt + TaxAmnt; }
        }

        public InvoiceLineModel()
        {
            this.LineNum = InvoiceViewModel.getNextLineNum();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}

You can do that in code first approach but not in the database first, so, basically, you need to update the database first. 您可以在代码优先方法中执行此操作,但不能在数据库中执行此操作,因此,基本上,您需要首先更新数据库。 You are using a database first approach so you need the following steps: 您正在使用数据库第一种方法,因此您需要以下步骤:

1) Add new fields in the table in your case ("InvoiceLine") in the database directly 1)直接在数据库的表格中添加新字段(“InvoiceLine”)

2) Once you add you can update EF Generated Model by right-clicking and updating model 2)添加后,您可以通过右键单击并更新模型来更新EF生成模型

3) While updating you need to select the table checkbox. 3)更新时,您需要选择表格复选框。 (Image attach for your ref) (图片附件为您的参考) 在此输入图像描述

在此输入图像描述

4) Once you update the EF model you can have properties available in EF generated class 4)更新EF模型后,您可以在EF生成的类中使用属​​性

Helping Resource: https://entityframework.net/ef-database-first 帮助资源: https//entityframework.net/ef-database-first

All done. 全部做完。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Entity Framework db-first 项目中添加非聚集索引? - How to add nonclustered indexes in Entity Framework db-first project? 实体框架DB-First,实现继承 - Entity Framework DB-First, implement inheritance 实体框架4 DB-First依赖注入? - Entity Framework 4 DB-First Dependency Injection? ASP.NET Core DB-first Entity Framework Core 不能自动增加主键 - ASP.NET Core DB-first Entity Framework Core can not auto increment primary key 努力单元测试实体框架6.1.3 DB-first - Effort unit testing Entity framework 6.1.3 DB-first 如何修改使用DB-first Entity Framework创建的枚举 - How to modify an enumeration created with DB-first Entity Framework 将实体(和相应的表)添加到EF DB优先模型 - Adding an entity (and respective table) to EF DB-first model PostgreSQL EF.Core DB-First Invalid DB Model:找不到与 CLR 类型为“bool”的属性“Db.Order.High”的关系类型的映射 - PostgreSQL EF.Core DB-First Invalid DB Model: No mapping to a relational type can be found for property 'Db.Order.High' with the CLR type 'bool' 我可以在 Entity Framework Code First 中使用来自我的域模型的谓词吗? - Can I use predicates from my Domain Model in Entity Framework Code First? 如何将基本表添加到使用Entity Framework创建TPH模型的现有实体? - How can I add a base table to an existing entity creating TPH model using Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM