【问题标题】:Adding & saving new records returns SqlException: Invalid column name 'BillingTypes_Id'添加和保存新记录返回 SqlException: Invalid column name 'BillingTypes_Id'
【发布时间】:2019-12-17 00:58:19
【问题描述】:

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

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

Invalid Column name when using savechanges() in entity framework

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

使用 VS 2017,EF 6.1.3

视图模型:

    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");

    }

Billings 交易表结构:(包含 id、datetime、cost、billingtypeId)

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

计费类型表结构:(包含id、name)。例如:(1,“酒精”)

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

【问题讨论】:

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


【解决方案1】:

在模型的 BillingTypesId 属性之上,添加这个装饰器:

[ForeignKey("BillingTypes ")]

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

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

这样 EF 将知道 BillingTypesId 是 BillingTypes 的外键。

【讨论】:

    【解决方案2】:

    我认为问题在于Entity Framework无法推断BillingTransactionBillingTypes之间的外键关系。您可以覆盖 OnModelCreating 并使用 fluent api,也可以像这样使用数据注释:

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

    添加using System.ComponentModel.DataAnnotations.Schema;

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-03
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        • 2019-11-20
        • 1970-01-01
        相关资源
        最近更新 更多