如何将外键添加到实体

时间:2015-01-19 19:37:30

标签: c# entity-framework

  

保存不公开其关系的外键属性的实体时发生错误。 EntityEntries属性将返回null,因为无法将单个实体标识为异常源。

我首先使用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; }
}

我不知道为什么会发生这种情况,因为在Transaction表的数据库中我看到有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
}

1 个答案:

答案 0 :(得分:1)

尝试添加

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

在您的交易类中。