如何给实体添加外键

How to add foreign key to entity

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception.

我首先使用的是 EF 代码,但在尝试执行此操作时出现上述错误:

public ActionResult CreateTransaction(TransactionViewModel model)
    {
        try
        {
            if (model.MemberId != 0 && model.SubcategoryId != 0 & model.CategoryId != 0)
            {
                Transaction t = new Transaction();
                t.Amount = model.Amount;
                t.Comment = model.Comment;
                t.Date = DateTime.Now;
                t.TransactionSubcategory = db.Subcategories.Find(model.SubcategoryId);//and i have error in this line
                //i tried also this code below but it's the same error
                //db.Subcategories.Find(model.SubcategoryId).Transactions.Add(t);

                db.Members.Find(model.MemberId).Transactions.Add(t);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("CreateTransaction") ;
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            return RedirectToAction("CreateTransaction");
        }
    }

还有我的模型

public class Subcategory
{
    public Subcategory()
    {
        IsGlobal = false;
        Transactions = new List<Transaction>();
    }
    public int Id { get; set; }

    public string Name { get; set; }

    public TransactionType TypeOfTransaction { get; set; }

    public Category OwnerCategory { get; set; }

    public List<Transaction> Transactions { get; set; }

    public bool IsGlobal { get; set; }
}

public class Transaction
{
    public int Id { get; set; }

    public Decimal Amount { get; set; }

    public DateTime Date { get; set; }

    public string Comment { get; set; }

    public Subcategory TransactionSubcategory { get; set; }

    public Member OwnerMember { get; set; }
}

我不知道为什么会这样,因为在事务的数据库中 table 我看到有 FK 列。 如果您需要,还有模型的其余部分

public class Budget
{
    public Budget()
    {
        Members = new List<Member>();
    }

    public int Id { get; set; }

    public string BudgetName { get; set; }

    public string OwnerName { get; set; }

    public DateTime CreatedTime { get; set; }

    public List<Member> Members { get; set; }
}

public class Category
{
    public Category()
    {
        IsGlobal = false;
    }
    public int Id { get; set; }

    public string Name { get; set; }

    public List<Subcategory> Subcategories { get; set; }

    public Budget OwnerBudget { get; set; }

    public bool IsGlobal { get; set; }
}

public class Member
{
    public Member()
    {
        Transactions = new List<Transaction>();
    }

    public Budget OwnerBudget { get; set; }

    public int Id { get; set; }

    public string Name { get; set; }

    public DateTime CreatedTime { get; set; }

    public List<Transaction> Transactions { get; set; }
}

public enum TransactionType
{
    Income,
    Expenditure
}

尝试添加

public int TransactionSubcategoryId { get; set; }
public int OwnerMemberId { get; set; }

在您的交易中class。