添加并保存新记录将返回SqlException:无效的列名'BillingTypes_Id'

时间:2019-12-16 15:56:22

标签: c# asp.net-mvc entity-framework

我在这个问题上停留了2天:(

我正在为我和妻子做“家庭预算服务”。但是,当我尝试将更改保存到数据库时,仍然出现“ SqlException:无效的列名'BillingTypes_Id'”。我见过很多带有相似标题的案例,主要是:

Invalid Column name when using savechanges() in entity framework

我的问题发生在 _context.SaveChanges();

使用VS 2017,EF 6.1.3

ViewModel:

    public class BillingViewModel
    {
        public IEnumerable<BillingTypes> BillingTypes { get; set; }

        public int Id { get; set; }

        public DateTime TransactionDate { get; set; }

        [Display(Name = "BillingTypes")]
        [Required]
        public int BillingTypesId { get; set; }

        [Required(ErrorMessage = "Please enter cost.")]
        public double Cost { get; set; }

        public BillingViewModel(BillingTransaction billingTransaction)
        {
            Id = billingTransaction.Id;
            TransactionDate = billingTransaction.TransactionDate;
            BillingTypesId = billingTransaction.BillingTypesId;
            Cost = billingTransaction.Cost;
        }

        public BillingViewModel()
        {
            Id = 0;
            TransactionDate = DateTime.Now;
            Cost = 0;
        }

型号:

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

    public BillingTypes BillingTypes { get; set; }

    public DateTime TransactionDate { get; set; }

    [Display(Name = "BillingTypes")]
    [Required]
    public byte BillingTypesId { get; set; }

    [Required(ErrorMessage = "Please enter cost.")]
    public double Cost { get; set; }
}

public class BillingTypes
{
    [Required]
    public int Id { get; set; }

    public string Name { get; set; }
}

BillingsController:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Save(BillingTransaction billingTransaction)
    {
        if (!ModelState.IsValid)
        {
            var types = _context.BillingTypes.ToList();

            var viewModel = new BillingViewModel
            {
                BillingTypes = types
            };

            return View("BillingForm", viewModel);
        }

        if (billingTransaction.Id == 0)
            _context.BillingTransactions.Add(billingTransaction);
        else
        {
            var transactionInDb = _context.BillingTransactions.Single(b => b.Id == 
            billingTransaction.Id);

            transactionInDb.Id = billingTransaction.Id;
            transactionInDb.TransactionDate = billingTransaction.TransactionDate;
            transactionInDb.BillingTypesId = billingTransaction.BillingTypesId;
            transactionInDb.Cost = billingTransaction.Cost;
        }
        _context.SaveChanges();

        return RedirectToAction("Index", "Billings");

    }

帐单交易表结构:(包含ID,日期时间,费用,billingtypeId)

[屏幕] [1]:https://i.stack.imgur.com/dyLZk.png

计费类型表结构:(包含ID,名称)。例如:(1,“酒精”)

[屏幕] [2]:https://i.stack.imgur.com/i4x3F.png

3 个答案:

答案 0 :(得分:1)

我认为问题在于实体框架无法推断BillingTransactionBillingTypes之间的外键关系。您可以覆盖OnModelCreating并使用流利的api,也可以使用如下数据注释:

[Display(Name = "BillingTypes")]
[Required]
[ForeignKey("BillingTypes")]
public byte BillingTypesId { get; set; }

添加using System.ComponentModel.DataAnnotations.Schema;

答案 1 :(得分:1)

在模型的BillingTypesId属性顶部,添加以下装饰器:

[ForeignKey("BillingTypes ")]

您的最终代码应如下所示:

[Display(Name = "BillingTypes")]
[Required]
[ForeignKey("BillingTypes ")]
public byte BillingTypesId { get; set; }

通过这种方式,EF将知道BillingTypesId是BillingTypes的外键。

答案 2 :(得分:0)

尝试从public byte BillingTypesId { get; set; }类中删除BillingTransaction属性。您已经通过添加public BillingTypes BillingTypes { get; set; }属性来定义关系。