[英]How to persist a property of type an interface in Entity Framework Core?
In my application, expenses could have many resources like Bank account or cash money in your safe or a family member likes to give you money to buy whatever you want...在我的申请中,费用可能有很多资源,比如银行账户或保险箱里的现金,或者家庭成员喜欢给你钱买你想要的任何东西......
So I have a Bank class which it has multiple accounts and a safe class.所以我有一个 Bank 类,它有多个帐户和一个安全类。 Account and safe are marked with IFinancialResource
.帐户和保险箱标有IFinancialResource
。
public interface IFinancialResource
{
public decimal Balance { get; }
public void SetBalance(decimal amount);
public void Deposite(decimal amount);
public void Withdraw(decimal amount);
}
public class Bank : BaseEntity, IAggregateRoot
{
public string Name { get; private set; }
public string WebSite{ get; private set; }
public Address Address { get; private set; }
private readonly List<Account> _accounts = new List<Account>();
public IReadOnlyCollection<Account> Accounts => _accounts.AsReadOnly();
}
public class Account : BaseEntity, IFinancialResource
{
public string AccountNumber { get; private set; }
public decimal Balance { get; private set; }
public AccountType AccountType { get; private set; }
public int AccountTypeId { get; private set; }
//to make a not null foreign key
public int BankId { get; private set; }
public Account(int accountTypeId, string accountNumber, decimal balance)
{
AccountNumber = accountNumber;
SetBalance(balance);
AccountTypeId = accountTypeId;
}
public void SetBalance(decimal amount)
{
Balance = amount;
}
public void Deposit(decimal amount)
{
Balance += amount;
}
public void Withdraw(decimal amount)
{
if ((Balance - amount) < 0)
return;
Balance -= amount;
}
}
public class Safe : BaseEntity, IFinancialResource, IAggregateRoot
{
public string Name { get; private set; }
public decimal Balance { get; private set; }
public Safe(string name, decimal balance)
{
Validate.NullOrEmpty(name, nameof(name));
Name = name;
SetBalance(balance);
}
public void SetBalance(decimal amount)
{
Validate.OutOfRange(amount, nameof(amount), 0, decimal.MaxValue);
Balance = amount;
}
public void Deposit(decimal amount)
{
Validate.OutOfRange(amount, nameof(amount), 0, decimal.MaxValue);
Balance += amount;
}
public void Withdraw(decimal amount)
{
Validate.OutOfRange(amount, nameof(amount), 0, decimal.MaxValue);
if ((Balance - amount) < 0)
return;
Balance -= amount;
}
}
Now I want to define a class named FinancialActivity
which has a property of type IFinancialResource
interface as the source of each expense.现在我想定义一个名为FinancialActivity
的类,它有一个IFinancialResource
接口类型的属性作为每个费用的来源。
The problem is Entity Framework Core doesn't persist interface properties.问题是 Entity Framework Core 不保留接口属性。 How should I persist each expense's resource in the FinancialActivity
class?我应该如何在FinancialActivity
类中保留每个费用的资源?
public class FinancialActivity : BaseEntity, IAggregateRoot
{
public string ActivityDate { get; private set; }
public string Subject { get; private set; }
public double Latitude { get; private set; }
public double Longitude { get; private set; }
public Address Address { get; private set; }
public IFinancialResource FinancialResource { get; private set; }
}
This isn't an EF Core question so much as a database persistence question.这与其说是一个 EF Core 问题,不如说是一个数据库持久性问题。 Navigation properties represent foreign keys, so how would you model this in SQL Server (for example)?导航属性表示外键,那么您将如何在 SQL Server 中对此进行建模(例如)? If you can model it in SQL Server, you can almost always represent it in EF Core.如果您可以在 SQL Server 中对其进行建模,则几乎总是可以在 EF Core 中表示它。 If there isn't a concrete representation to model in the database, how would the foreign keys be set up?如果在数据库中没有具体的模型表示,外键将如何设置?
There are quite a bit of missing classes in your example, so I couldn't recreate it, but the basic answer is that you have to be able to map your models to persistent types, so you will want to map to the base class and use derived classes for your unique types.您的示例中缺少很多类,因此我无法重新创建它,但基本答案是您必须能够将模型映射到持久类型,因此您需要映射到基类和为您的独特类型使用派生类。 I also noticed you are mixing business logic with your persistence entities, which is usually not a good idea.我还注意到您将业务逻辑与持久性实体混合在一起,这通常不是一个好主意。 (Note: I say usually because there aren't any absolutes in development). (注意:我说通常是因为在开发中没有任何绝对的东西)。 Might want to look at the decorator pattern to accomplish the different methods that you are implementing in your different types.可能想要查看装饰器模式来完成您在不同类型中实现的不同方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.